I'm trying to create a page that shows all the subcategories of a specific category. So in context, I have a Parent Category titled 'Destinations'. I'd like to be able to click on Destinations and be led to a page showing a list of the subcategories or Countries. Here's an example of what I'm trying to achieve minus the map at the top - .
In the ideal world, the Category page would have the same layout as this page. You can see what I mean from the example above.
It would make the navigation on the site like this Destinations > Country > Individual Post.
I'm using CPT UI and have set up a custom post type and categories called 'Destinations' and then have a bunch of sub-categories of the countries.
Any help would be greatly appreciated!
Regards,
I'm trying to create a page that shows all the subcategories of a specific category. So in context, I have a Parent Category titled 'Destinations'. I'd like to be able to click on Destinations and be led to a page showing a list of the subcategories or Countries. Here's an example of what I'm trying to achieve minus the map at the top - https://www.vagabrothers/destinations.
In the ideal world, the Category page would have the same layout as this page. You can see what I mean from the example above.
It would make the navigation on the site like this Destinations > Country > Individual Post.
I'm using CPT UI and have set up a custom post type and categories called 'Destinations' and then have a bunch of sub-categories of the countries.
Any help would be greatly appreciated!
Regards,
Share Improve this question asked Jan 14, 2020 at 0:36 user6880554user6880554 11 Answer
Reset to default 0Depending on the hierarchy of WordPress templates, you can create a template: category-$slug.php
, in your case, it will be: category-destinations.php
(to mention that the slug must be the same name) .
So this template will have the priority if we click on the link category destinations.
I used Twenty Nineteen theme.
Templete named : category-destinations.php
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<?php
$subcat = get_categories(array(
'orderby' => 'name',
'parent' => $wp_query->queried_object->term_id
));
if (count($subcat)):
foreach ($subcat as $key => $value) {
echo '<h2>' . $value->name .'</h2>';
echo '<a title="'.$value->name.'" '
. 'href="' . esc_url(get_category_link($value->term_id)) .'">'
.esc_html($value->cat_name,"twentynineteen").'</a>';
}
else :
esc_html__("No subcategories found","twentynineteen");
endif;
?>
</main><!-- #main -->
</div><!-- #primary -->
I created a Destination category, and I added: Aciform, arrangement, ..... as a subcategory and the relults :
Here I have recovered that the name and the links of the category. if you want to add an image to the subcategory, you need a custom code, because WordPress by default does not offer the possibility of adding images for the category.