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?