-1

I'm creating a custom post type like this:

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' );

And the categories like this:

    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' );

I'm trying to get the parent category on the custom post type but I have tried many solutions with no luck. For example this one:

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

But the problem with this solution is that it displays all categories instead of just the parent. Any idea on how to do it?

2

Your CPT and your CT init seem to be right, but the way you get the taxonomy terms is incomplete. If you look in WordPress Codex, the function get_terms(); accepts two parameters:

  • $taxonomies
  • $args

If you want to get only parent terms you have to specified it in your $args array.

$args = array(
 'parent' => 0 // queries only top level terms of a specify taxonomy or taxonomies ( first argument, rememberer? )
);

So in your case, try this:

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

Hope it's helps!

1

The top most terms, also known as the parents, has a parent value of 0. Any other term that has a value other than 0 set as the parent is a child term of that specific term which ID is set as parent

If you look at the get_terms() documentation, you can make use of this parent parameter

parent

(integer) Get direct children of this term (only terms whose explicit parent is this value). If 0 is passed, only top-level terms are returned.

Default is an empty string.

With this all in mind, to get only the top level terms, you can pass parent=0 as one of the arguments to get_terms

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

It's late, but I found a solution after a pair of hours of search and I'm happy to share it. My solution works for any custom post type, so it won't be needed to specify the custom post type.

So, if you are outside the loop, and you need to get the parent category name (or any parameter) of a custom post type, this is the way:

$post_type = get_post_type();
$obj = get_post_type_object( $post_type );
$custom_post_title = $obj->labels->singular_name;
echo $custom_post_title;

PD: I don't know why this question is poorly documented on the net.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.