WordPress: the_terms()没有按照预期呈现我的分类法链接,为什么?



我在我的WordPress插件中使用一个数据表来显示标题和分类法,但我没有让它在输出中正常工作。

这条线:

$return .="<td>" . the_terms( $post->ID , 'authors', '', ', ' ) . "</td>";

结果:

<a href="www.example.com/author1/" rel="tag">Author 1</a>
<a href="www.example.com/author2/" rel="tag">Author 2</a>

,使<td></td><td></td>为空。

我想要这个结果:

<td>
<a href="www.example.com/author1/" rel="tag">Author 1</a>
</td>
<td>
<a href="www.example.com/author2/" rel="tag">Author 2</a>
</td>

多个作者之间用:,

分隔解决方案吗?

the_terms()函数立即回显值,这就是为什么您的post标签在您的td标签之外呈现。

来自文档:

显示列表中某篇文章的关键词。

当您想要将返回值赋给一个变量时,您希望使用get_the_terms()来代替,例如(未经测试,但应该让您走上正确的轨道):

$term_obj_list = get_the_terms( $post->ID, 'authors' );
$term_links = array();
if ( $term_obj_list && ! is_wp_error( $term_obj_list ) ) :
foreach( $term_obj_list as $term ):
$term_links[] = '<a href="' . esc_attr( get_term_link( $term->slug, 'authors' ) ) . '">' . $term->name . '</a>';
endforeach;
$return .= "<td>" . join( ', ', $term_links ) . "</td>";
endif;

最新更新