正在将自定义分类拖入页面,但它没有超链接

时间:2020-01-30 作者:The Chewy

我有一个WP_Query 类,并作为其中的一部分,我想显示此帖子类型的自定义分类法(类别)。

我目前的解决方案引入了分类名称,但不包括超链接?我希望当用户单击此自定义分类法时,它会显示此分类法的相关归档页面。

我在页面上使用的与分类法相关的特定代码片段是:

<p class="cat">
    <?php  $terms = get_the_terms( $post->ID, \'news_categories\' );
            foreach ( $terms as $term ) {
            echo $term->name;
            }
    ?>
</p>
以上代码段是这个较大代码块的一部分:

<?php 
    $homePageNews = new WP_Query(array(
        \'posts_per_page\' => 3,
        \'post_type\'=> \'news\'
    ));

    while(  $homePageNews->have_posts()){
            $homePageNews->the_post(); ?>

            <article class="article top-article lastest-news-article">
            <a href="<?php the_permalink(); ?>">
            <?php the_post_thumbnail(\'post-thumbnail\', 
                [\'class\' => \'image latest-top-image hover-class\']); ?>
            </a>   
                <p class="cat">
                    <?php  $terms = get_the_terms( $post->ID, \'news_categories\' );
                            foreach ( $terms as $term ) {
                            echo $term->name;
                            }
                    ?> <a href=""></a>
                </p>
                <div class="content-wrapper content-wrapper-mobile-padding">
                    <h3 class="td no-bottom-margin hover-class"><a title="<?php the_title(); ?>" class="td top-latest-heading" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <hr>
                    <p><?php the_content(); ?></h3></p>
                </div>
            </article>

<?php } ?>

<?php  wp_reset_postdata(); ?>

1 个回复
最合适的回答,由SO网友:Dip Patel 整理而成

@Chewy您可以尝试下面的代码片段。

<p class="cat">
    <?php
    $terms = get_the_terms($post->ID, \'news_categories\');
    foreach ($terms as $term) {
        $term_id    = $term->term_id;
        $term_name  = $term->name;
        $term_link  = get_term_link( $term_id );
        echo "<a href=\'".$term_link."\'>".$term_name."</a>";
    }
    ?> 
</p>
请检查并让我知道这是否有效。

如果没有,请告诉我,我一定会帮助你的。

谢谢

相关推荐

Wp_Term_Taxonomy.Parent引用了什么?

我最初的想法是,它引用了另一种分类法(即wp_term_taxonomy.parent 到wp_term_taxonomy.term_taxonomy_id). Corcel这样的怪物built under this assumption. 然而,我done some digging 大多数信息似乎表明它实际上引用了wp_terms.term_id. 在Corcel的案例中,我认为这种巧合是可行的——WordPress将设置term_id 以及term_taxonomy_id 到相同的值。如果您弄乱了数据