I am trying to show in a single post child category from a selected category. For some single post, I have a category that is corresponding to an area. I want to show that category in a special area of my single post template.
I tried several ways but I can’t do it. So far here I am back at the start calling the post categories :
$regions = get_the_category( array(
'parent' => '62',
));
Anyone can help me? Thank you
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Well in the end I used custom taxonomy, and it’s also more simple to manage. I post as I did :
//hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires
add_action( 'init', 'create_regions_nonhierarchical_taxonomy', 0 );
function create_regions_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' => _x( 'Regions', 'Where are located activities or place of interests' ),
'singular_name' => _x( 'Region', 'Regions singular name' ),
'search_items' => __( 'Search Regions' ),
'popular_items' => __( 'Popular Regions' ),
'all_items' => __( 'All Regions' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Region' ),
'update_item' => __( 'Update Region' ),
'add_new_item' => __( 'Add New Region' ),
'new_item_name' => __( 'New Region Name' ),
'separate_items_with_commas' => __( 'Separate regions with commas' ),
'add_or_remove_items' => __( 'Add or remove regions' ),
'choose_from_most_used' => __( 'Choose from the most used regions' ),
'menu_name' => __( 'Regions' ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('regions','post',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'region', 'with_front' => false),
));
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0