-
bsrvt6
Hi there,
Can you please guide me create a sub-categories page under the main category like this page https://www.expertreviews.co.uk/home-garden
Under the sub-categories page, I want to show further the blogs like in the URL
can you please let me know the steps I have to take to create a sub-categories page under the main category
Thanks
-
Fernando
Hello there,
To clarify, are you referring to separating the Parent and child categories by section on a Category page?
-
bsrvt6
Hi there,
I am looking to create a parent category page like Home & Garden and under this page, I want to create multiple subcategories like BATHROOM, BEDROOM, BOOKS, ENERGY & PLANTS HEATING LIGHTING.
I want to show a box image with little content under parent category page like this https://www.expertreviews.co.uk/home-garden
-
Fernando
It’s just showing Home & Garden posts in general. Then, there’s this “filter”: https://share.getcloudapp.com/kpulPg40
What you can do is create a Block Element – Content or Loop Template for the layout of the posts. Reference: https://docs.generatepress.com/article/block-element-content-template/
Then, you’ll need a custom code for the filter.
-
bsrvt6
Hi there,
Thanks for your response. I am not very good on WP so can you please explain to me in bit of detail how can create a filter or that sub-category
Thanks
-
Hi there,
There’s no easy way for the filter section, it will require custom PHP coding and some CSS to style it.
If you’d like to pursue this, here’re the steps:
1. Go to appearance > elements, create a hook element, paste the below code:
<?php $parent_cat_id = get_queried_object_id(); // Get the ID of the current parent category $child_categories = get_categories(array( 'child_of' => $parent_cat_id, // Retrieve child categories of the parent category 'hide_empty' => 0, // Show empty categories as well )); if ($child_categories) { echo '<ul class="sub-categories">'; foreach ($child_categories as $category) { echo '<li><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></li>'; } echo '</ul>'; } ?>
2. Make sure tick the Execute PHP box, choose
generate_after_archive_title
for the hook, and choosepost category archive
as the location. -
bsrvt6
Hi there,
Thank you for that,
I can not find option to choose post category archive and there is no box to tick the Execute PHP box
This is shown at the bottom Unable to execute PHP as DISALLOW_FILE_EDIT is defined.
I am using Info Template from Generate Library
-
This is shown at the bottom Unable to execute PHP as DISALLOW_FILE_EDIT is defined.
Ah, in that case, you can not use PHP in hook elements due to your configuration.
Let’s change the plan, here’s the updated code that you can add to a plugin like Code Snippet or
functions.php
of your child theme if you are using one.function list_of_child_categories() { if (is_category()){ $parent_cat_id = get_queried_object_id(); // Get the ID of the current parent category $child_categories = get_categories(array( 'child_of' => $parent_cat_id, // Retrieve child categories of the parent category 'hide_empty' => 0, // Show empty categories as well )); if ($child_categories) { echo '<ul class="sub-categories">'; foreach ($child_categories as $category) { echo '<li><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></li>'; } echo '</ul>'; } } } add_action('generate_after_archive_title','list_of_child_categories');
- You must be logged in to reply to this topic.