how to list parent terms only

时间:2011-09-18 作者:rakesh

我有以下代码来列出所有术语,但我需要一个代码,其中我只能列出“父术语”,因为它们也都有子术语

<?php
// output quick links list of countries
$terms = get_terms(\'apptypes\');
if (count($terms)) {
echo "<h3>Quick links</h3>";
echo "<p style=\\"text-align:center;\\">";
}
$i=0; // counter for printing separator bars
foreach ($terms as $term) {
$wpq = array (\'taxonomy\'=>\'apptypes\',\'term\'=>$term->slug);
$query = new WP_Query ($wpq);
$article_count = $query->post_count;
echo "<a href=\\"#".$term->slug."\\">".$term->name."</a>";
// output separator bar if not last item in list
if ( $i < count($terms)-1 ) {
echo " | " ;
}
$i++;
}
if (count($terms)) {
echo "</p>";
}
?>

1 个回复
SO网友:ugurcem

get_terms(\'apptypes\',\'orderby=id&parent=0\');
parent参数用于获取此项的直接子项(仅限显式父项为此值的项)。如果0 如果通过,则只返回顶级术语。默认值为empty string.

看见Codex.

结束

相关推荐

WP_SET_OBJECT_TERMS超时?

我有一个大约350个术语的列表,我正试图设置为一个职位。这是我的代码:add_action(\'init\', \'add_cities_to_story\'); function add_cities_to_story() { $cities_list = get_term_children(\'115\', \'location\'); // MA wp_set_object_terms(\'632\', $cities_list, \'location\')