Expert Group of programming
Loading...

Helpful all type of Page listing Functions for WordPress

Posted by : Heycode Tech | Posted in : wordpress, WordPress Functions | Jul 12,2016 | 0 comments

As I know that when we are developing a custom theme or plugin the many times we need the page listings. Some time we need the : Parent Page only : child Page only : Page that have child only : All page and SubPage listings : Page id by page Title : etc.

So today, here I ‘m writing all these types of page functions on here one by one.

Fotor0712214551

1: All the child page listing of current page.

Sometime, we need to get all the child page only of the current page. Here is the function which is used to get the page listing of the child page.

<?php
$mypages = get_pages( array( 'child_of' => get_the_ID(), 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
if($mypages){
  echo '<ul class="heycodetech-pagelistings">';
foreach( $mypages as $page ) {     
  ?>
  <li class="page-loop">
  <a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a>
  </li>
  <?php
  }
echo '</ul>';
}else{
  echo "sorry,No child page of this current page";
}
?>

This above functions will use for listing of child page. Please send me the suggestions if you have anymore.

2: List all the parent page only.

Sometime we need to display all the parent page only i.e in this listing, we don’t include any child page. Here is the functions which is used.

<?php
$mypages = get_pages( array( 'hierarchical' =>'1','parent' => 0, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
if($mypages){
  echo '<ul class="heycodetech-pagelistings">';
foreach( $mypages as $page ) {    
  ?>
  <li class="page-loop"> 
  <a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a>
  </li>
  <?php
  }
echo '</ul>';
}else{
  echo "sorry,No page found.";
}
?>

This above functions will use for listing of parent page only. Please send me the suggestions if you have anymore.

3: List of parent Pages that have child page.

It is a critical condition, sometime we need those parent page that have child page and we will exclude of the parent page which have not child page.

Here is the functions

<?php
$mypages = get_pages( array( 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
if($mypages){
  echo '<ul class="heycodetech-pagelistings">';
  
foreach( $mypages as $page ) {  

//get all the child page of this current page
   //if it have the child page : then list in.
   $children = get_pages('child_of='.$page->ID);
   if( count( $children ) != 0 ) {
  ?>
  <li class="page-listings"> 
  <a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a>
  </li>
  <?php
   }
  }
echo '</ul>';
}else{
  echo "sorry,No page found.";
}
?>

This above functions is used to list of parent page that have the child page.

4: List of all Pages .

Sometime, we need to display all the page listing i.e parent page and subpage both listings.One more requirement, the child is just right align with parent page. Here I ‘m writing the functions which is used to display all the pages.

<?php
$mypages = get_pages( array( 'hierarchical' =>'1','parent' => 0,'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
if($mypages){
  echo '<ul class="heycodetech-pages">';
  
foreach( $mypages as $page ) {  

//get all the child page of this current page
   //if it have the child page : then list in.
  ?>
 <li class="loop-page"> 
  <a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a>
 
   <?php
   $children = get_pages('child_of='.$page->ID);
   if( count( $children ) != 0 ) {
  ?>
  <ul class="child-pages">
   <?php 
foreach( $children as $child ) { 
    ?>
  <li class="child-loop-page"> 
  <a href="<?php echo get_page_link( $child->ID ); ?>"><?php echo $child->post_title; ?></a>
  </li>
  <?php } ?>
   </ul>
  <?php
   }
   ?>
 </li>
   <?php
  }
echo '</ul>';
}else{
  echo "sorry,No page found.";
}
?>

This above functions will use for listing of all pages i.e child page and parent page both. Please send me the suggestions if you have anymore.

5: Page id by page Title

Sometime, we need the page id by the page title. Below functions is used for getting it.

Example your page title :  About
$page    = get_page_by_title( 'About' );
$pageid  = $page->ID;

Please use and review all the pages functions and let me know if need any modification. Please also comment here and appreciate the author.

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.