猿问

在 Single.php 中显示带有描述的帖子标签

我想在 single.php 中显示带有描述的单个帖子标签。


我搜索了这个,最接近的解决方案如下。但是这段代码列出了博客的所有标签和描述。


$tags = get_tags( array( 'hide_empty' => false ) );

if ($tags) {

    foreach ($tags as $tag) {

        if ($tag->description) {

            echo '<dt><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a></dt><dd>' . $tag->description . '</dd>';

        } 

    } 

}

我只需要列出带有描述的帖子标签。(它应该排除没有描述的帖子标签。)


例如:


我们在数据库中有4500 多个标签。


200 多个标签有描述。


示例单个帖子上有7 个标签。


其中只有4 个有描述。


结果:


我只需要在 single.php 中显示4 个标签。


侃侃尔雅
浏览 165回答 1
1回答

三国纷争

你可以试试下面的代码,它可以在 single.php 中工作<?php$tags = wp_get_post_tags(get_the_ID());if ($tags) {&nbsp; &nbsp; foreach ($tags as $tag) {&nbsp; &nbsp; &nbsp; &nbsp; if ($tag->description) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<dt><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a></dt><dd>' . $tag->description . '</dd>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }&nbsp;}?>
随时随地看视频慕课网APP
我要回答