获取当前页面的分类名称

时间:2017-10-05 作者:Kasperr

我正在尝试以与获取术语名称相同的方式获取分类名称:$术语->名称-这可能吗?

但是$taxonomy->name不会显示任何内容$taxonomy单独显示slug,但我需要分类的标签/名称。

// 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 );
    $taxonomynames = get_queried_object();
    $out           = " <ul\'>";

    foreach ( $taxonomies as $taxonomy ) {
        $out .= "<li\'><span\'>" . $taxonomy->name . ": ";
        $out .= "</span>";

        // get the terms related to post
        $terms = get_the_terms( $post->ID, $taxonomy );

        if ( ! empty( $terms ) ) {
            foreach ( $terms as $term ) {
                $out .= \'<a href="\' . $term->slug . \'">\' . $term->name . \'</a> \';
            }
        }

        $out .= "</li>";
    }

    $out .= "</ul>";

    return $out;
}

1 个回复
SO网友:socki03

这个get_object_taxonomies function 只将所有已注册分类法的slug返回到该post类型,所以您就快到了。实际上,您需要使用get_taxonomy()

...
foreach ( $taxonomies as $taxonomy ) {
    $tax_object = get_taxonomy( $taxonomy );
    $out .= "<li\'><span\'>" . $tax_object->name . ": ";
    $out .= "</span>";
    ...

结束

相关推荐

Custom sub taxonomy order by

在单类别页面中,特定的父类别子类别按slug或order wise显示,我会尝试,但我的代码中有错误,请检查我的代码并告诉我如何可能。$term_children = get_term_children( $EM_Category->id, \'event-categories\' ); if ( count($term_children) > 0 ) { foreach ( $term_children as $child ) { $term