获取分类链接

我正在尝试在 Wordpress 中创建分类法的链接,这样当您单击一个时,它会将您带到 Wordpress 页面,列出该分类法的所有帖子,但我不太明白。你会怎么做?


当前代码:


    <?php

    $post_type = get_post_type(get_the_ID());   

    $taxonomies = get_object_taxonomies($post_type);   

    $taxonomy_names = wp_get_object_terms(get_the_ID(), $taxonomies,  array("fields" => "names")); 

    if(!empty($taxonomy_names)) :

      foreach($taxonomy_names as $tax_name) : ?>              

        <h2 class="text-lg"><?php echo $tax_name; ?> </h2>

        //planning to have the link here (<a href="?">?</a>)

      <?php endforeach;

    endif;

  ?>

感谢所有帮助!


蛊毒传说
浏览 107回答 1
1回答

繁华开满天机

试试这个代码,它在我这边工作正常<?php&nbsp; &nbsp; $post_type = get_post_type(get_the_ID());&nbsp; &nbsp;&nbsp; &nbsp; $taxonomies = get_object_taxonomies($post_type);&nbsp; &nbsp;&nbsp; &nbsp; $taxonomy_names = wp_get_object_terms(get_the_ID(), $taxonomies);&nbsp; &nbsp; if(!empty($taxonomy_names)) :&nbsp; &nbsp; &nbsp; &nbsp; foreach($taxonomy_names as $tax_name) :&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2 class="text-lg"><?php echo $tax_name->name; ?> </h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="<?php echo get_term_link($tax_name->term_id); ?>">texonomy link </a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; endforeach;&nbsp; &nbsp; endif;?>
打开App,查看更多内容
随时随地看视频慕课网APP