如何从wp_get_post_terms返回的数组中提取单个值

时间:2013-01-22 02:54:22

标签: wordpress return-value taxonomy term

在Wordpress中,我想在分类法“章节”中显示附加到帖子的术语。 我可以使用

获取帖子的分类信息
$sectiondata = wp_get_post_terms($post->ID, 'chapters', array("fields" => "all"));

然后使用

print_r $sectiondata;

我可以显示返回值的数组。

但是,如何回显页面中术语“名称”的值? 我认为这应该是这样的:

echo $sectiondata->name;

但是没有返回,所以我显然不明白如何从数组中提取这个值。我一直在寻找示例,并没有看到任何解释如何在页面上显示值的内容,或者更好地说明了如何从数组中提取值。我尝试过使用

的简单php方法
print($sectiondata['name']);

但这也不会返回任何东西。

在哪里可以找到有关如何从数组中提取值的说明。

由于

1 个答案:

答案 0 :(得分:2)

问题是你没有循环返回的数组。在我向您展示如何使用wp_get_post_terms执行此操作之前,您是否尝试过使用get_terms函数?我相信这可能是一种更好的方法:

$terms = get_terms('chapters');
echo '<ul>';
foreach ($terms as $term) {
    echo '<li><a href="'.get_term_link($term->slug, 'species').'">'.$term->name.'</a></li>';
}
echo '</ul>';

来源:http://codex.wordpress.org/Function_Reference/get_terms

...

如果这对您不起作用,请查看如何使用wp_get_post_terms执行相同的操作:

echo "<ul>";
$terms = wp_get_post_terms( $post->ID, 'chapters');
foreach($terms as $term) {
    echo "<li><a href='".get_term_link($term)."' title='".$term->name."'>".$term->name."</a></li>";
}
echo "</ul>"; 

http://codex.wordpress.org/Function_Reference/wp_get_post_terms

我希望这会帮助你!如果上面的代码示例存在任何问题,请告诉我。

相关问题