我在WordPress上创建了自定义分类,我想在列表中显示帖子上的当前帖子分类。
我正在使用以下代码显示名为“Job Discipline”的自定义分类:
<ul>
<?php $args = array('taxonomy' => 'job_discipline'); ?>
<?php $tax_menu_items = get_categories( $args );
foreach ( $tax_menu_items as $tax_menu_item ):?>
<li>
Job Discipline: <a href="<?php echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>">
<?php echo $tax_menu_item->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
这只是我想列出的许多分类法之一。
问题是上面的代码显示的所有“职位规则”至少有一个帖子而不是当前的邮政分类。
如何理清这个问题?
答案 0 :(得分:24)
以下是来自Codex的修改后的代码(请参阅下面的链接),该代码将显示当前帖子的所有分类以及附加条款:
<?php
// get taxonomies terms links
function custom_taxonomies_terms_links() {
global $post, $post_id;
// get post by post id
$post = &get_post($post->ID);
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies($post_type);
$out = "<ul>";
foreach ($taxonomies as $taxonomy) {
$out .= "<li>".$taxonomy.": ";
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy );
if ( !empty( $terms ) ) {
foreach ( $terms as $term )
$out .= '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a> ';
}
$out .= "</li>";
}
$out .= "</ul>";
return $out;
} ?>
这是这样使用的:
<?php echo custom_taxonomies_terms_links();?>
如果当前帖子具有分类country
和city
,则输出可能如下所示:
<ul>
<li> country:
<a href="http://example.com/country/denmark/">Denmark</a>
<a href="http://example.com/country/russia/">Russia</a>
</li>
<li> city:
<a href="http://example.com/city/copenhagen/">Copenhagen</a>
<a href="http://example.com/city/moscow/">Moscow</a>
</li>
</ul>
食典委的原始代码示例:
http://codex.wordpress.org/Function_Reference/get_the_terms#Get_terms_for_all_custom_taxonomies
希望这会有所帮助 - 我相信你可以根据你的项目进行调整; - )
但是,如果我只想展示其中的一些而不是全部呢? 此外,我想自己命名,而不是给它分类 带下划线的名字。知道我怎么能实现这个目标?
以下是实现此目的的一项修改:
function custom_taxonomies_terms_links() {
global $post;
// some custom taxonomies:
$taxonomies = array(
"country"=>"My Countries: ",
"city"=>"My cities: "
);
$out = "<ul>";
foreach ($taxonomies as $tax => $taxname) {
$out .= "<li>";
$out .= $taxname;
// get the terms related to post
$terms = get_the_terms( $post->ID, $tax );
if ( !empty( $terms ) ) {
foreach ( $terms as $term )
$out .= '<a href="' .get_term_link($term->slug, $tax) .'">'.$term->name.'</a> ';
}
$out .= "</li>";
}
$out .= "</ul>";
return $out;
}
答案 1 :(得分:2)
以防万一有人想要按父母分组显示它们。
它与上面的答案基本相同。我在另一篇文章中使用了这个答案: https://stackoverflow.com/a/12144671 将它们分组(按id和按父母分组)。
修改了与对象一起使用的函数:
function object_group_assoc($array, $key) {
$return = array();
foreach($array as $object) {
$return[$object->$key][] = $object;
}
return $return;
}
最终功能:
// get taxonomies terms links
function custom_taxonomies_terms_links() {
global $post, $post_id;
// get post by post id
$post = &get_post($post->ID);
// get post type by post
$post_type = $post->post_type;
// get post type taxonomies
$taxonomies = get_object_taxonomies($post_type);
$out = "<ul>";
foreach ($taxonomies as $taxonomy) {
$out .= "<li>".$taxonomy.": ";
// get the terms related to post
$terms = get_the_terms( $post->ID, $taxonomy );
if ( !empty( $terms ) ) {
$terms_by_id = object_group_assoc($terms, 'term_id');
$terms_by_parent = object_group_assoc($terms, 'parent');
krsort($terms_by_parent);
foreach ( $terms_by_parent as $parent_id => $children_terms ){
if($parent_id != 0){//Childs
//Add parent to out string
$parent_term = $terms_by_id[$parent_id][0]; //[0] because object_group_assoc return each element in an array
$out .= '<li><a href="' .get_term_link($parent_term->slug, $taxonomy) .'">'.$parent_term->name.'</a>';
//Add children to out string
$out .= '<ul>';
foreach ($children_terms as $child_term) {
$out .= '<li><a href="' .get_term_link($child_term->slug, $taxonomy) .'">'.$child_term->name.'</a></li>';
}
$out .= '</ul></li>';
} else {//parent_id == 0
foreach ($children_terms as $child_term) {
if(!array_key_exists($child_term->term_id, $terms_by_parent)){//Not displayed yet becouse it doesn't has children
$out .= '<li><a href="' .get_term_link($child_term->slug, $taxonomy) .'">'.$child_term->name.'</a></li>';
}
}
$out .= '</ul></li>';
}
}
}
$out .= "</li>";
}
$out .= "</ul>";
return $out;
}
使用相同的方式:
<?php echo custom_taxonomies_terms_links();?>
注意:只需使用一个级别的子项。