显示WP中的所有类别,并以粗体显示(Display all categories in WP, with the selected in bold)

编程入门 行业动态 更新时间:2024-10-28 06:25:01
显示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); ?>

更多推荐

本文发布于:2023-07-22 01:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215537.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并以   粗体   类别   WP   bold

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!