php 在WordPress中显示帖子的分类

qojgxg4l  于 2023-01-24  发布在  PHP
关注(0)|答案(1)|浏览(158)

我有一个页面,我所有的可湿性粉剂的职位显示。类别的职位显示罚款,但当我试图也显示分类,它不工作。
这是可行的:

<span><i class="fa fa-list"></i><?php $categories = get_the_category();
                                $separator = ", ";
                                $output = '';
                                if($categories){
                                    if($link_on_cat == "yes"){ 
                                        foreach($categories as $category){
                                            $output .= '<a href="' .get_category_link($category->term_id) .'">' . $category->cat_name . '</a>'  . $separator;
                                        }
                                    } else {
                                        foreach($categories as $category){
                                            $output .=  $category->cat_name . $separator;
                                        }
                                    }
                                    echo trim($output, $separator);
                                } ?></span>

这是行不通的:

<span><i class="fa fa-list"></i><?php $categories = get_the_category();
                                $separator = ", ";
                                $output = '';
                                global $post, $post_id;
                                $post_type = $post->post_type;
                                $taxonomies = get_object_taxonomies($post_type);
                             
                                if($categories){
                                    if($link_on_cat == "yes"){ 
                                        foreach($categories as $category){
                                            $output .= '<a href="' .get_category_link($category->term_id) .'">' . $category->cat_name . '</a>'  . $separator;
                                        }
                                         foreach ($taxonomies as $taxonomy) {  $out .= "<li>".$taxonomy.": ";  $terms = get_the_terms( $post->ID, $taxonomy );
        if ( !empty( $terms ) ) {
            foreach ( $terms as $term )
                $out .= '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a> ';
                                         }     }
                                    } else {
                                        foreach($categories as $category){
                                            $output .=  $category->cat_name . $separator;
                                        }
                                    }
                                    echo trim($output, $separator);
                                } ?></span>

我当然是做错了,但是错了什么?

46scxncf

46scxncf1#

试试这个代码

$taxonomies = get_object_taxonomies($post_type);
$separator = ", ";
$output = '';
foreach ($taxonomies as $taxonomy) { 
   $output .= "<li>".$taxonomy.": ";  
   $terms = get_the_terms( $post->ID, $taxonomy );
   if ( !empty( $terms ) ) {
     foreach ( $terms as $term )
        $output .= '<a href="' .get_term_link($term->slug, $taxonomy) 
       .'">'.$term->name.'</a> ';
     }
    }
  echo $output;

相关问题