在WordPress中显示当前文章的自定义分类法

8

我已经在WordPress上创建了自定义分类法,我想在文章中以列表形式显示当前文章的分类法。

我使用以下代码显示名为“工作学科”的自定义分类法:

<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>

这只是我想列出的众多分类之一。

问题在于上面的代码显示了所有至少有一个帖子的“职业学科”,而不是当前帖子分类。

我该如何解决这个问题?


问题截图:https://docs.google.com/file/d/0BwVSqH5yzcN-Wm1YY29CdUxfbkk/edit - Bhanu Chawla
你可以在http://wordpress.stackexchange.com上获得更好的答案。 - cpilko
2个回答

29

如何显示当前文章的分类法和术语

以下是来自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();?>

演示输出

如果当前帖子具有分类法 countrycity,则输出可能如下所示:

<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>

参考资料

Codex中的原始代码示例:

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;
} 

很好!这是结果截图:https://docs.google.com/file/d/0BwVSqH5yzcN-d3dSLVdPeUpPUUk/edit 但如果我只想显示其中一些而不是所有的呢?此外,我想自己命名它们,而不是给出带有下划线的分类名称。你有什么想法可以实现吗? - Bhanu Chawla
这里有一个更新的版本,可以隐藏未分配的分类法:http://wordpress.stackexchange.com/questions/130993/list-taxonomy-terms-in-current-post-current-category/ - birgire
谢谢!对我来说真正重要的是:get_the_terms( $post->ID, $taxonomy ); 我之前只用了 get_terms($taxonomy),这是不正确的。 - tkrn
很高兴能帮到你 @tkm - birgire

2

如果有人想按父级分组显示它们,可以使用以下方法。

基本上与上面的答案相同。我使用了另一篇帖子中的答案: https://dev59.com/YnI95IYBdhLWcg3wtwRe#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();?>

注意:仅与一级子项一起使用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接