function show_subpages() {
global $post;
$subpages = wp_list_pages( array(
'echo' =>0,
'title_li' =>'',
'depth' =>2,
'link_before' => '— ',
'child_of' => ( $post->post_parent == 0 ? $post->ID : $post->post_parent),
'exclude' => ( $post->post_parent == 0 ? '' : $post->ID)
));
if ( !empty($subpages) ) {
echo '<ul id="subpages" class="wrapper">';
echo $subpages;
echo '</ul>';
}
}
wp_list_pages
supports an exclude
parameter. In the above, I’ve simply added 'exclude' => ( $post->post_parent == 0 ? '' : $post->ID)
to the parameter array.
It works analogous to the way you are already feeding values to child_of
:
If the post does not have a parent, we feed exclude
an empty string. If it has one, we feed it the current post ID.
Related Posts:
- How can I programmatically create “child” pages on theme activation?
- List child pages of specific page using shortcode
- How to get specified parent page title in my function
- Sort and display pages with specific custom field (not tag)
- Why isn’t is_page working when I put it in the functions.php file?
- Get the ID of the page a menu item links to?
- Disable visual editor on one specific page
- Shortcode producing headers already sent error
- Breadcrumbs showing Parent and Child Pages
- How to display error messages using WP_Error class?
- Slugs as breadcrumbs for Pages
- Check if has any sidebar active on current page
- Remove Page Title from Static Frontpage
- Change ul class=”children” for wp_list_pages?
- Target a certain page within wordpress backend (admin) i.e. Pages > About
- Breadcrumb how i can display page title with parent > child title with permalink ? any Idea
- Pages should have priority when using add_rewrite_rule
- Enqueuing Script in functions.php vs on the page
- Programmatically set page template based on page ID
- Query children and parent title
- Delete pages and Create default pages for all new network sites
- Manipulate Output of wp_list_something: select menu instead of li’s
- how to change link of some wordpress pages
- Make menu structure match page heirarchy on page parent change
- Change the slug of a particular page every x hours
- wp_insert_posts keeps adding multiple pages
- How to include local menu based on page id?
- Batch update menu_order attribute alphabetically
- is_account_page() change to page slug
- Don’t delete a page if it holds users
- List child pages by slug not ID?
- Display post shortcode content in the sidebar?
- Why is my page feed returning empty content?
- Function only on a specific page
- Combining ‘depth’ with ‘include’ in wp_list_pages()
- Functions For Calling Specific Elements
- Get page id or status by private pages?
- new_excerpt_more link not working properly
- What is the text that appears beside the page titles in the list of pages in the WP admin?
- Enqueue script on every page except one
- How to store a number (coming from cookie) into query vars for later usage in other filters?
- Using depth=n in wp_list_pages
- Do we need to change our child function.php to require/include child dir files when we add an over-riding file.php into the child theme
- Keep page hierarchy in wp_list_pages, even if on a child or grandchild
- How do I register a new settings page? [closed]
- Only echo Page Content (not page title & page content together)
- How to hightlight all ancestor menu items of a child page NOT displayed in menu
- Dynamic menu wp_list_pages displaying only current branch including: current page, his siblings and its childs
- get content from page through AJAX
- Check if page has subpages
- How to apply the ‘current_page_item’ class to an archive page in `wp_list_pages()`?
- How to assign php file(template) to several pages with same prefix page name/slug
- Remove the delete link from pages without a plugin
- Display list of pages that contain a certain string within the slug
- Change is_front_page() to is_page() to display ‘hero’ on entire site?
- Define an extra field for all pages (with no plugin?)
- Custom field not updating via functions upon publishing
- Why is wp_list_pages altering $post->ID of the page?
- Preventing PHP Execution in Parent Theme
- wp_list_pages change of children and anchor of parent
- Using array page name together with page id to deregister script
- How to add a class name to the ancestor of a post?
- Get current post’s child page?
- How to make in post Parent page children page list nummbered
- How to add an excerpt and read more link to a page?
- My website is generating weired url parameters of paginated pages
- custom function works on page.php template but not in functions.php
- Override categories with Pages (block theme)
- WordPress login set cookie that survive browser exit (wp_signon function)
- wp_nav_menu displaying all pages
- WordPress 6.0 upgrade caused my custom function to stop working
- How to avoid saving empty data to sql while using add_meta_box
- How to Display WooCommerce Product Price in WooCommerce Archive
- How to change text color depending on the number value (Using javascript)
- is_main_query() always returning false
- Woocommerce Variable Product Dropdown for Custom Shop Template
- Is this a backup door?
- Create Custom URL structure for specific Post category using Post ID instead of post name
- Include widget file in functions.php of child theme
- WordPress comment processing . Default unapproved comments detection before posting
- Astra theme – cannot set short product description under the product title (product name)
- Warning at top of website & top menu gone
- Featured Image and Tags problem
- Is posible to redirect a few URL?
- Need help making a short code work
- Add custom taxonomy using JS in post editor
- Use a filter for wp_robots to block CPT/feed/
- WordPress: Highlight search result exact matches
- Custom image sizes showing in Classic Editor only when upladed directly to post
- Trigger email alert if file downloaded
- Completely Disable WordPress RSS Feeds
- Default click “I have read and agree to the website terms and conditions”
- How to use wp_enqueue_script properly?
- How to hide content on mobile in the functions.php file – woocommerce
- Woocommerce Default sorting product by product type
- Displaying registered sidebar after post content
- Hide Author By-Line if After Certain Date
- Search anything from a Child and Sub-Pages of their Parent page
- This code is supposed to only allow user to be authenticated if accountVerified is equal to 1, but it still allows user to be authenticated otherwise
- Get webpack to work on child theme