Only display sub-nav if present - Wordpress Solution
[Solved] - Only display sub-nav if present - Wordpress Solution |
---|
Try following codes |
Hi Experts,
I built a custom page template that uses an if/else statement to display the child pages as a 'sub-nav' if there are child pages present. This logic works fine if you are on the parent page, but once you click on one of the child pages, the sub-nav goes away. How can I modify my code so that the sub-nav will show on all pages within the section?
Solution - 1A simple way would be to modify the conditional at the very beginning of the statement to also include a check for the parent ID: becomes First it uses the post's parent id, and if it doesn't have a parent then we use the current page ID. Everything else runs as normal. Solution - 2Well that is just normal since when you open the child page, the if returns false and the code within it is not executed. You will have to use get_post_ancestors($post->ID); to find out if the post you are currently reading has a parent. if it does then you get the child posts of the current post's parent just as you already do. try with something like this:
|
Date: 2022-10-21 05:23:03 |
Category: Wordpress |