Logo Pupungbp.com

Need a WordPress expert help?

Get Current Child Page with get_children

Last Updated: June 27, 2020 | Reading Time: < 1 minute

There are many method to display current child page in WordPress, this function is usefull to do the same command. Just Paste this code in functions.php

function sb_child_page_menu( $current_parent ) {

// Display Subpages on the parent and list subpages of the current parent if on the child page

	if(wp_get_post_parent_id($current_parent)) {
		$parent_ID = wp_get_post_parent_id( $current_parent );
	} else {
		$parent_ID = $current_parent;
	}

// Get the Childpages / Subpages using get_children function

	$arg = array(
		'post_parent' => $parent_ID,
	);

	$childrens = get_children( $arg );

	if( !empty($childrens) ) {
		echo '<div class="sub-page">';
		foreach ( $childrens as $children ) {
			echo '<a href="'.get_permalink($children->ID).'">'.$children->post_title.'</a>';
		}
		echo '</div>';
	}
}

Call the function within the template file:

sb_child_page_menu( get_the_ID() );

more documentation about get_children.

Looking for an affordable WordPress solution?

We offer budget-friendly WordPress website development services to meet your needs. Prices start at $10.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.