Expert Group of programming
Loading...

Pages and Child pages listing in wordpress

Posted by : Heycode Tech | Posted in : wordpress, WordPress Functions | Nov 8,2015 | 0 comments

Recently one of my user’s asked me for page listing as well as child page listing in WordPress. So here I ‘m describing code about the page listing as well as child page listing and more .

Here I ‘m describing the page listing in different form like via direct listing , short-code listing

1: All pages listing via direct code : For listing all the pages, we use the default function of the WordPress. It is called : get_pages(); : .So here is the complete function.

<?php
 echo '<ul>';
$pages = get_pages(); 
  foreach ( $pages as $page ) {
  	$option = '<li>';
	$option .= $page->post_title;
	$option .= '</li>';
	echo $option;
  }
echo '</ul>';
 ?>

So you can use this in any default template or any custom template of the WordPress.

2: All pages listing via ShortCode: Now WordPress have given the functionality for creating the shortcode which is used in any page, post,custom post type or any file directly . Now in that time,it is an modern way for doing anything.

Here I ‘m developing the shortcode : [pagelisting] :, these are :-

<?php
function pagelisting_callback($atts, $content = "" ){
 	$atts = shortcode_atts( array(), $atts, 'pagelisting' );
      //creating an buffer string
ob_start();
echo '<ul>';
$pages = get_pages(); 
  foreach ( $pages as $page ) {
  	$option = '<li>';
        $option .= '<a href="' . get_page_link( $page->ID ) . '">';
	$option .= $page->post_title;
        $option .= '</a>';
	$option .= '</li>';
	echo $option;
  }
echo '</ul>';
 $output = ob_get_contents();
  ob_get_clean();
return $output;
}
add_shortcode('pagelisting','pagelisting_callback');
  ?>

You need to past this code in file :functions.php : and call the ShortCode :[pagelisting] : in any page,post or Custom Post Type.
Note : For developing the code of different shortcode , Visit on this Post :- Visit Here

3:Displaying Child pages of the current page in post format
Many times, we need to show the list of child pages, so it is necessary and very useful code for listing the child page of current page.

<?php
	$mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );

	foreach( $mypages as $page ) {		
		$content = $page->post_content;
		if ( ! $content ) // Check for empty page
			continue;

		$content = apply_filters( 'the_content', $content );
	?>
		<h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
		<div class="entry"><?php echo $content; ?></div>
	<?php
	}	
?>

You can directly use this code in any template file.

default-image

We hope this article helped you list child pages,all pages in WordPress. Let us know if you have any questions or feedback by leaving a comment below.

Tags:

Author information - Heycode Tech

Every world can ask question and my website registered user give the suggestion.

See all posts by Author

Connect to author

About us

Heycodetech is a platform where you can ask any questions related to the programming like PHP,jQuery, MYSQL,HTML , CSS and moreHere is the Expert group of programming.