我有一个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(); ?>
最合适的回答,由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>
请检查并让我知道这是否有效。
如果没有,请告诉我,我一定会帮助你的。
谢谢