dongmuzhan4705 2018-05-03 12:23
浏览 35
已采纳

php生成所有wordpress类别的链接

Im having a little problem. I want to display all categories connected to a wordpress post.

I found several codes and plugins, but all of them have problems. Some dont display correct anchor text, others dont link to correct url. So i took the working parts from different places and put them together to the code below, and its working. The problem is that it does not generate links to ALL connected categories, it only show link to 1 single category (first one that was connected to the post)

what is wrong with the code ? Why will it not show links to all connected categories. I have been working on it for 2 days now, and i give up.

<?php
$the_cat = get_the_category();
$category_name = $the_cat[0]->cat_name;
$category_link = get_category_link( $the_cat[0]->cat_ID );
?>
<?php 
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>
<a class="button" href="<?php echo get_category_link( $category_id ); ?>"title=”<?php echo $category_name ?>”  ><?php echo $category_name ?></a>

</div>
  • 写回答

3条回答 默认 最新

  • duanlinzhen7235 2018-05-03 13:38
    关注

    If you want to show the category that is related with the posts on post details page and archive page add this code in your child theme functions.php and call the function where you want to display it.

    function postsCategories()
    {
        $categories_list = get_the_category_list( esc_html__( ', ', 'your-text-domain' ) );
        $allowed_tags_before_after=array('span' => array('class'=>array()),'i'=>array('class'=>array()),'a'=>array('class'=>array(),'href'=>array(),'rel'=>array()));
    
            if ( $categories_list )
            {
                    printf(wp_kses(sprintf(__('<span class="blogmeta cat-links"> <i class="fa fa-folder-open"></i> %1$s </span>','your-text-domain'), $categories_list ),$allowed_tags_before_after)); 
            }
    }
    

    Call in the template : <?php printf(__('%s','your-text-domain'),postsCategories()); ?>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?