0

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.com/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,

0

Depending 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 :

enter image description here

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.

  • Hey, thanks for getting back to me. Can you give me an example of what the code might look like in total? I created a category-destinations.php and added the code. This is what I've got get_header(); ?> <h1>Test</h1> <p><?php $subcat = get_categories(array( 'orderby' => 'name', 'parent' => $wp_query->queried_object->term_id )); ?></p> <p><?php echo ' . $subcat . ';?></p> <?php get_footer(); ?> – user6880554 Jan 14 at 2:39
  • ok done. I updated the code. to test the code: 1 - create a template named: category-destinations.php, 2- create a category slug destinations, 3 - Add subcategories for Destinations. – Younes.D Jan 14 at 3:35

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.