微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 在WordPress中显示当前的帖子自定义分类

我已经在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>

这只是我想列出的许多分类法之一.

问题是上面的代码显示的是所有“职务规则”,其中至少有一个职位而不是当前的职位分类.

我该如何解决这个问题?

如何显示当前的分类后和术语

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

演示输出

如果当前帖子具有分类法国家和城市,则输出可能如下所示:

<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

希望这有帮助 – 我相信你可以适应你的项目;-)

更新

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?

以下是实现此目的的一个修改

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://www.jb51.cc/php/136623.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐