- This topic has 3 replies, 2 voices, and was last updated 4 years ago by
Tom.
-
AuthorPosts
-
March 21, 2019 at 7:28 pm #846192
kunal
Hi David and Tom,
I have some secondary navigation categories. What I would like to do have a box (almost like a Table of Contents box), that goes under the title of the category.
The link that I provided is an illustration of what I would like to do
GeneratePress 2.2.2March 22, 2019 at 10:23 am #846789Tom
Lead DeveloperLead DeveloperHi there,
Should be pretty easy.
1. Create a new Hook Element: https://docs.generatepress.com/article/hooks-element-overview/
2. Set the hook to after_archive_description
3. Check the Execute PHP checkbox.
4. Add this as your content:<?php $current_cat = get_queried_object(); $args = array( 'child_of' => $current_cat->term_id ); $categories = get_categories( $args ); if ( ! empty( $categories ) ) { echo '<p>Find out more:</p>'; echo '<ul>'; foreach ( $categories as $category ) { echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>'; } echo '</ul>'; } ?>
5. In the Display Rules, set it to “Post Category Archive”.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentMarch 22, 2019 at 1:41 pm #846954kunal
Hi Tom, thanks for the reply… .but… still no luck! I put the code as above, and still no output. Only the environmental category has a child category, so only this category archive page should have an output, but there is no output of the children categories?
thanks Tom!
KunalMarch 22, 2019 at 3:59 pm #847030Tom
Lead DeveloperLead DeveloperAh, you’re using a Page Hero instead of the standard archive description.
In that case, you need to create a shortcode:
add_shortcode( 'subcat_links', function() { ob_start(); $current_cat = get_queried_object(); $args = array( 'child_of' => $current_cat->term_id ); $categories = get_categories( $args ); if ( ! empty( $categories ) ) { echo '<p>Find out more:</p>'; echo '<ul>'; foreach ( $categories as $category ) { echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>'; } echo '</ul>'; } return ob_get_clean(); } );
Then add the shortcode to the Header Element in the Elements area:
[subcat_links]
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-development -
AuthorPosts
- You must be logged in to reply to this topic.