Archived topic
Drop Down Menu
3 replies · Started by kunal on March 21, 2019
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
Hi 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".
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!
Kunal
Ah, 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]