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
- Define page template in wp_insert_post
- Child theme functions.php do I use php open and close tags?
- Enabling shortcodes for custom fields
- Passing variables to templates (alternatives to globalizing variables)
- Load post with a different template?
- Most efficient way to get custom database records from 20 buttons and 20 tables?
- Empty Date Returned on my function
- Woocommerce Attributes pulling in wrong data
- How to get the post_name when in preview?
- Can’t check if a post has thumbnail adding filter to get_post_metadata()
- Echo tags used to describe the theme
- How to Display Frontend CSS for Administrator only
- How can I change a function in a parent theme via a child themes functions.php
- Custom Nav Walker sub-menu
- How do I remove the date and category form my portfolio pages
- Get list of all custom tanonomy id
- Modify post filter to set custom number of posts per page and exclude child posts
- Changing parent element’s class or style
- User function to return multiple get_post_meta()
- Native gallery custom html output
- WordPress wraps span tags into p tags
- Auto scroll to id on page load on all pages except home page. Only do this on mobile
- How to add schema markup to WordPress menu function
- contact form in template with jquery,validate and ajax
- Header menu aligned right on all pages except for single-post page [closed]
- How to create an array if ’empty’?
- i can’t use wordpress functions in ajax loaded php file
- Remove External Links from WordPress posts Using add_filter() in Theme functions.php
- Adding A New Widget to WordPress Disables the Existing Widgets
- Childpage title + content on Parent Page
- Problem in outputting shortcode
- How to get tags and categories?
- Archive.php, and post_is_in_descendant_category
- Retrieving next_post_link() and previous_post_link() in functions.php