获取自定义帖子类型父类别

时间:2014-12-12 作者:wpuser

我正在创建如下自定义帖子类型:

function btp_work_register_post_type() {
    $args = array(
        \'label\'     => __(\'Works\', \'btp_theme\'),
        \'labels\'    => array(
            \'name\'                  => __( \'Works\', \'btp_theme\' ),
            \'singular_name\'         => __( \'Work\', \'btp_theme\' ),
            \'add_new\'               => __( \'Add new\', \'btp_theme\' ),
            \'all_items\'             => __( \'All Works\', \'btp_theme\' ),
            \'add_new_item\'          => __( \'Add new Work\', \'btp_theme\' ),
            \'edit_item\'             => __( \'Edit Work\', \'btp_theme\' ),
            \'new_item\'              => __( \'New Work\', \'btp_theme\' ),
            \'view_item\'             => __( \'View Work\', \'btp_theme\' ),
            \'search_items\'          => __( \'Search Works\', \'btp_theme\' ),
            \'not_found\'             => __( \'No Works found\', \'btp_theme\' ),
            \'not_found_in_trash\'    => __( \'No Works found in Trash\', \'btp_theme\' ),
            \'parent_item_colon\'     => __( \'Parent Work\', \'btp_theme\' ), 
            \'menu_name\'             => __( \'Cartera\', \'btp_theme\' ),
        ),
        \'public\'                => true,
        \'publicly_queryable\'    => true,
        \'exclude_from_search\'   => false,
        \'show_ui\'               => true,
        \'show_in_menu\'          => true,
        \'hierarchical\'          => false,
        \'supports\'              => array(\'title\', \'editor\', \'excerpt\', \'custom-fields\', \'thumbnail\', \'comments\', \'revisions\'),
        \'has_archive\'           => true,                
        \'rewrite\'               => array(\'slug\' => \'cartera\'),
        \'query_var\'             => \'btp_work\',
        \'can_export\'            => true,
        \'show_in_nav_menus\'     => true,
    );

    /* Apply custom filters (this way Child Themes can change some arguments) */
    $args = apply_filters( \'btp_pre_register_post_type\', $args, \'btp_work\' );

    register_post_type( \'btp_work\', $args );
}
add_action( \'init\', \'btp_work_register_post_type\' );
以及以下类别:

    function btp_work_register_taxonomies() {   
        /* Compose arguments for btp_work_category */
        $args = array(
            \'label\'                 => __(\'Work Category\', \'btp_theme\'),
            \'labels\'                => array(
                \'name\'                  => __( \'Work Categories\', \'btp_theme\' ),
                \'singular_name\'         => __( \'Work Category\', \'btp_theme\' ),
                \'search_items\'          => __( \'Search Work Categories\', \'btp_theme\' ),
                \'popular_items\'         => __( \'Popular Work Categories\', \'btp_theme\' ),
                \'all_items\'             => __( \'All Work Categories\', \'btp_theme\' ),
                \'parent_item\'           => __( \'Parent Work Category\', \'btp_theme\' ),
                \'parent_item_colon\'     => __( \'Parent Work Category:\', \'btp_theme\' ),
                \'edit_item\'             => __( \'Edit Work Category\', \'btp_theme\' ), 
                \'update_item\'           => __( \'Update Work Category\', \'btp_theme\' ),
                \'add_new_item\'          => __( \'Add New Work Category\', \'btp_theme\' ),
                \'new_item_name\'         => __( \'New Work Category\', \'btp_theme\' ),
                \'menu_name\'             => __( \'Work Categories\', \'btp_theme\' ),
            ),        
            \'query_var\'             => \'btp_work_category\',         
            \'rewrite\'               => array(\'slug\' => \'work-category\', \'with_front\' => true),
            \'public\'                => true,
            \'hierarchical\'          => true,
            \'show_in_nav_menus\'     => true,
            \'show_ui\'               => true,
            \'show_tagcloud\'         => true,
        );  

        $slug = sanitize_title( btp_theme_get_option_value( \'btp_work_category_rewrite_slug\' ) );
        if( strlen( $slug ) ) {
            $args[ \'rewrite\' ][ \'slug\' ] = $slug;
        }

        /* Apply custom filters (this way Child Themes can change some arguments) */
        $args = apply_filters( \'btp_pre_register_custonomy\', $args, \'btp_work_category\' );

        register_taxonomy( \'btp_work_category\', array(\'btp_work\'), $args );


        /* Compose arguments for btp_work_tag */
        $args = array(          
            \'label\'                 => __(\'Work Tag\', \'btp_theme\'),
            \'labels\'                => array(
                \'name\'                  => __( \'Work Tags\', \'btp_theme\' ),
                \'singular_name\'         => __( \'Work Tag\', \'btp_theme\' ),
                \'search_items\'          => __( \'Search Work Tags\', \'btp_theme\' ),
                \'popular_items\'         => __( \'Popular Work Tags\', \'btp_theme\' ),
                \'all_items\'             => __( \'All Work Tags\', \'btp_theme\' ),
                \'parent_item\'           => __( \'Parent Work Tag\', \'btp_theme\' ),
                \'parent_item_colon\'     => __( \'Parent Work Tag:\', \'btp_theme\' ),
                \'edit_item\'             => __( \'Edit Work Tag\', \'btp_theme\' ), 
                \'update_item\'           => __( \'Update Work Tag\', \'btp_theme\' ),
                \'add_new_item\'          => __( \'Add New Work Tag\', \'btp_theme\' ),
                \'new_item_name\'         => __( \'New Work Tag\', \'btp_theme\' ),
                \'menu_name\'             => __( \'Work Tags\', \'btp_theme\' ),                  
            ),  
            \'query_var\'             => \'btp_work_tag\',
            \'rewrite\'               => array(
                \'slug\' =>\'work-tag\', 
                \'with_front\' => true
            ),
            \'public\'                => true,
            \'hierarchical\'          => false,
            \'show_in_nav_menus\'     => true,
            \'show_ui\'               => true,
            \'show_tagcloud\'         => true,        
         );  

        $slug = sanitize_title( btp_theme_get_option_value( \'btp_work_tag_rewrite_slug\' ) );
        if( strlen( $slug ) ) {
            $args[ \'rewrite\' ][ \'slug\' ] = $slug;
        }

        /* Apply custom filters (this way Child Themes can change some arguments) */
        $args = apply_filters( \'btp_pre_register_taxonomy\', $args, \'btp_work_tag\' );

        register_taxonomy( \'btp_work_tag\', array(\'btp_work\'), $args );  
    }
    add_action( \'init\', \'btp_work_register_taxonomies\' );
我正在尝试获取自定义帖子类型的父类别,但我尝试了许多解决方案,但都没有成功。例如,这个:

<?php

$taxonomy = \'btp_work_category\';
$terms = get_terms($taxonomy); // Get all terms of a taxonomy

if ( $terms && !is_wp_error( $terms ) ) :
?>
    <ul>
        <?php foreach ( $terms as $term ) { ?>
            <li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
        <?php } ?>
    </ul>
<?php endif;?>
但这种解决方案的问题是,它显示所有类别,而不仅仅是父类别。你知道怎么做吗?

3 个回复
SO网友:iEmanuele

您的CPT和CT init似乎是正确的,但您获取分类术语的方式是不完整的。如果你进去看看WordPress Codex, 函数get\\u terms();接受两个参数:

如果只想获取父术语,必须在$args数组中指定它。

$args = array(
 \'parent\' => 0 // queries only top level terms of a specify taxonomy or taxonomies ( first argument, rememberer? )
);
因此,在您的情况下,请尝试以下方法:

<?php

$taxonomy = \'btp_work_category\';
$terms = get_terms($taxonomy, array( \'parent\' => 0 ) ); // Get all top level terms of a taxonomy

if ( $terms && !is_wp_error( $terms ) ) :
?>
    <ul>
        <?php foreach ( $terms as $term ) { ?>
            <li><a href="<?php echo get_term_link($term->slug, $taxonomy); ?>"><?php echo $term->name; ?></a></li>
        <?php } ?>
    </ul>
<?php endif;?>
希望有帮助!

SO网友:Pieter Goosen

最重要的术语,也称为家长,有parent 的值0. 具有以下值的任何其他术语0 set as the parent是ID设置为parent的特定术语的子术语

如果你看看get_terms() documentation, 你可以利用这个parent 参数

parent

(整数)获取此项的直接子项(仅限显式父项为此值的项)。如果通过0,则只返回顶级术语。

默认值为空字符串。

记住这一点,要想只获得顶级条款,你可以通过parent=0 作为get_terms

$terms = get_terms($taxonomy, \'parent=0\'); // Get all terms of a taxonomy

SO网友:Unix

虽然很晚了,但经过两个小时的搜索,我找到了一个解决方案,我很高兴与大家分享。我的解决方案适用于任何自定义帖子类型,因此无需指定自定义帖子类型。

因此,如果您在循环之外,并且需要获取自定义帖子类型的父类别名称(或任何参数),则可以这样做:

$post_type = get_post_type();
$obj = get_post_type_object( $post_type );
$custom_post_title = $obj->labels->singular_name;
echo $custom_post_title;
PD:我不知道为什么这个问题在网上记录得很差。

结束

相关推荐

Scrolling Posts with Ajax

这是整个谷歌都面临的一个非常普遍的问题,但我们处理这一问题的方式之间存在冲突。我使用本教程http://code.tutsplus.com/articles/getting-loopy-ajax-powered-loops-with-jquery-and-wordpress--wp-23232我不得不说,这是互联网上解释得最好的教程(对我来说)。到目前为止,我的工作做得很好,但也有一些例外,因为我缺乏知识,所以很难将其应用于我的网站。一些例外。如果只有一个post,那么停止加载其他条件的作者,我可以从页面