- This topic has 7 replies, 2 voices, and was last updated 1 year, 7 months ago by
David.
-
AuthorPosts
-
November 6, 2020 at 5:29 am #1520300
Grant
Hello,
I would like:
- To display a list of subcategories on a page, as hyperlinks.
- When you click the subcategory hyperlink you would then access all of the posts in that particular subcategory (I am using WP Show Posts for this)
Is there an easy way, perhaps a hook, to show a list of subcategories on a page? Maybe there’s a WP Show Posts trick I can’t work out?
Thanks,
Grant
November 6, 2020 at 7:18 am #1520663David
StaffCustomer SupportHi there,
how would you determine what list of sub categories were to be displayed on a Page?
If you were viewing a Category Archive then it would be possible to get the Parent Category and display a list of its children. But Pages won’t have any idea of what sub categories you want displayed.Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/November 6, 2020 at 7:43 am #1520691Grant
Getting the Parent Category to display a list of its children would work fine for me. How could I go about doing that?
November 6, 2020 at 9:14 am #1520805David
StaffCustomer SupportIs this to be displayed on a Static Page ? Or a Category Archive ?
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/November 6, 2020 at 9:35 am #1520836Grant
A static page would be preferable.
November 7, 2020 at 4:40 am #1521499David
StaffCustomer SupportHmmm… you could try creating a Shortcode that displays a list of Sub Categories of the given parent category.
Add this PHP Function to your site:
function display_subcategories( $atts ) { $atts = shortcode_atts( array( 'category' => null ), $atts ); $parent_cat_slug = get_category_by_slug( $atts['category'] ); $parent_cat_ID = $parent_cat_slug->term_id; $args = array( 'hierarchical' => 1, 'hide_empty' => 0, 'parent' => $parent_cat_ID, 'taxonomy' => 'category' ); $subcats = get_categories($args); ob_start(); echo '<ul class="sub-categories">'; foreach ($subcats as $subcat) { $link = get_term_link( $subcat->slug, $subcat->taxonomy ); echo '<li><a href="' . $link . '">' . $subcat->name . '</a></li>'; } echo '</ul>'; return ob_get_clean(); } add_shortcode( 'subcategory', 'display_subcategories' );
Then you can add the shortcode like so:
[subcategory category="category-slug"]
Change the
category-slug
to that of the parent category that you want to display the children of.Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/November 9, 2020 at 2:04 am #1523464Grant
Works great, thanks so much, David!
November 9, 2020 at 3:08 am #1523542David
StaffCustomer SupportAwesome – glad to be of help
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.