显示WP中的所有类别,并以粗体显示(Display all categories in WP, with the selected in bold)
我想在内容底部显示我的Wordpress中存在的所有类别,其中当前帖子的选定内容为粗体,如下所示。 例如,对于现有的帖子,我选择了3个现有的category2。
类别1 类别2类别3
我怎样才能做到这一点?
我的代码(现在只显示所选的类别):
<div class="entry-meta"> <span class="term-links"> <?php foreach ( get_the_terms( $post->ID, 'category') as $term ) : ?> <a href="<?php echo esc_url( get_term_link( $term->term_id ) ) ?>"><span class="<?php echo $term->slug ?>"><?php echo $term->name ?> </span></a> <?php endforeach; ?> </span> <style> .term-links .category2 { display: inline-block; font-weight:bold; </style>I want to display all the categories existing in my Wordpress in the bottom of the content, with the selected for the current post in bold, as follows. For example, for an existing post I selected category2 of 3 existing.
category1 category2 category3
How can I do this?
My code (now only display the selected category):
<div class="entry-meta"> <span class="term-links"> <?php foreach ( get_the_terms( $post->ID, 'category') as $term ) : ?> <a href="<?php echo esc_url( get_term_link( $term->term_id ) ) ?>"><span class="<?php echo $term->slug ?>"><?php echo $term->name ?> </span></a> <?php endforeach; ?> </span> <style> .term-links .category2 { display: inline-block; font-weight:bold; </style>最满意答案
所有类别的列表
在你的模板中添加下面的代码
<style type="text/css"> .single-product div.product .product_meta .product_cat ul li{ list-style-type:circle;} .single-product div.product .product_meta .product_cat ul li.current-cat{list-style-type:circle;} .single-product div.product .product_meta .product_cat ul li.current-cat a{display: inline-block;font-weight:bold;} </style> <?php global $post; $terms = get_the_terms( $post->ID, 'product_cat' ); $product_cat_id_array = array(); foreach ($terms as $term ) { $product_cat_id_array[] = $term->term_id; } $product_cat_id_string = implode(",",$product_cat_id_array); $args = array( 'child_of' => 0, 'current_category' => $product_cat_id_string, 'depth' => 0, 'echo' => 1, 'exclude' => '', 'exclude_tree' => '', 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'hide_empty' => 0, 'hide_title_if_empty' => false, 'hierarchical' => true, 'order' => 'ASC', 'orderby' => 'name', 'separator' => '', 'show_count' => 0, 'show_option_all' => '', 'show_option_none' => __( 'No categories' ), 'style' => 'list', 'taxonomy' => 'product_cat', 'title_li' => __( 'Categories' ), 'use_desc_for_title' => 0, ); wp_list_categories($args); ?>list of all category
add below code in your template
<style type="text/css"> .single-product div.product .product_meta .product_cat ul li{ list-style-type:circle;} .single-product div.product .product_meta .product_cat ul li.current-cat{list-style-type:circle;} .single-product div.product .product_meta .product_cat ul li.current-cat a{display: inline-block;font-weight:bold;} </style> <?php global $post; $terms = get_the_terms( $post->ID, 'product_cat' ); $product_cat_id_array = array(); foreach ($terms as $term ) { $product_cat_id_array[] = $term->term_id; } $product_cat_id_string = implode(",",$product_cat_id_array); $args = array( 'child_of' => 0, 'current_category' => $product_cat_id_string, 'depth' => 0, 'echo' => 1, 'exclude' => '', 'exclude_tree' => '', 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'hide_empty' => 0, 'hide_title_if_empty' => false, 'hierarchical' => true, 'order' => 'ASC', 'orderby' => 'name', 'separator' => '', 'show_count' => 0, 'show_option_all' => '', 'show_option_none' => __( 'No categories' ), 'style' => 'list', 'taxonomy' => 'product_cat', 'title_li' => __( 'Categories' ), 'use_desc_for_title' => 0, ); wp_list_categories($args); ?>更多推荐
发布评论