php 在 WordPress 中显示当前帖子自定义分类法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15502811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display current post custom taxonomy in WordPress
提问by Bhanu Chawla
I've created custom taxonomies on WordPress and I want to display the current post taxonomies on the post in a list.
我已经在 WordPress 上创建了自定义分类法,我想在列表中的帖子上显示当前的帖子分类法。
I'm using the following code to display a custom taxonomy named "Job Discipline":
我正在使用以下代码显示名为“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>
That's just one of many taxonomies I want to list.
这只是我想列出的众多分类法之一。
The problemis that the above code is displaying all the "Job Disciplines" which have at least one post and not the current post taxonomy.
问题是上面的代码显示了所有“工作学科”,这些“工作学科”至少有一个帖子,而不是当前的帖子分类法。
How can I sort out this issue?
我该如何解决这个问题?
回答by birgire
How to display current post taxonomies and terms
如何显示当前的帖子分类法和术语
Here is a modified code from the Codex (see link below) that will display all the taxonomies of the current post with attached terms:
这是来自 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;
} ?>
This is used like this:
这是这样使用的:
<?php echo custom_taxonomies_terms_links();?>
Demo output
演示输出
The output might look like this if the current post has the taxonomies countryand city:
如果当前帖子具有分类法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>
Reference
参考
The original code example in the Codex:
Codex 中的原始代码示例:
http://codex.wordpress.org/Function_Reference/get_the_terms#Get_terms_for_all_custom_taxonomies
http://codex.wordpress.org/Function_Reference/get_the_terms#Get_terms_for_all_custom_taxonomies
Hope this helps - I'm sure you can adapt this to your project ;-)
希望这会有所帮助 - 我相信你可以适应你的项目;-)
Update
更新
But what if I want to display only some of them and not all of them? Also, I would like to name them myself instead of it giving taxonomy names with underscores. Any idea how can I achieve that?
但是如果我只想显示其中的一部分而不是全部呢?另外,我想自己命名它们,而不是用下划线给出分类名称。知道我怎么能做到这一点吗?
Here is one modification to achieve that:
这是实现这一目标的一项修改:
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;
}
回答by Alicia Daza
Just in case someone wants to display them grouped by parent.
以防万一有人想按父项分组显示它们。
It's basically the same answer as above. I used this answer form another post: https://stackoverflow.com/a/12144671to group them (by id and by parent).
基本上和上面的答案是一样的。我在另一篇文章中使用了这个答案:https: //stackoverflow.com/a/12144671将它们分组(按 id 和按父级)。
Function modified to use it with objects:
修改函数以将其与对象一起使用:
function object_group_assoc($array, $key) {
$return = array();
foreach($array as $object) {
$return[$object->$key][] = $object;
}
return $return;
}
Final function:
最终功能:
// 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;
}
Used the same way:
使用方法相同:
<?php echo custom_taxonomies_terms_links();?>
Note: just working with one level children terms.
注意:仅使用一级子项。

