PHP 30
Pagechecker (Parent or Child) By micha on 12th December 2018 02:22:27 PM
  1. <?php
  2.  
  3.   /*********************************
  4.   *** Pagechecker - Parent/Child ***
  5.   *********************************/
  6.  
  7.   $is_child      = false;
  8.   $has_ancestors = false;
  9.   $enable_back   = false;
  10.  
  11.   /* If "$post->post_parent" value is 0, the page is a top level page */
  12.   if( is_page() && $post->post_parent > 0 ) {
  13.         $is_child       = true;
  14.         $has_ancestors  = true;
  15.         $enable_back    = true;
  16.         $ancestors      = get_ancestors( $post->ID, 'page' );
  17.         $ancestor_url   = get_permalink($ancestors[0]);
  18.         $depth          = count($ancestors);
  19.         //print_r($ancestors);
  20.  
  21.         if ( is_page() && $depth > 1 ) {
  22.           $enable_back  = true;
  23.         }
  24.   }
  25.  
  26.  
  27.   if ($is_child) {
  28.         /* Page IS a Child */
  29.         $args = array(
  30.                                   'post_type'      => 'page',
  31.                                   'posts_per_page' => -1,
  32.                                   'post_parent'    => $post->post_parent,
  33.                                   'order'          => 'ASC',
  34.                                   'orderby'        => 'menu_order'
  35.         );
  36.  
  37.         $child        = new WP_Query( $args );
  38.  
  39.         if ( $child->have_posts() ) : ?>
  40.           <div class="childpages list">
  41.                 <?php while ( $child->have_posts() ) : $child->the_post(); ?>
  42.                         <span id="parent-<?php the_ID(); ?>" class="parent-page">
  43.                                 <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
  44.                         </span>
  45.                 <?php endwhile; ?>
  46.           </div>
  47.         <?php endif; wp_reset_query();
  48.  
  49.   } else {
  50.         /* Page is NOT a child */
  51.         $children = get_pages( array( 'child_of' => $post->ID ) );
  52.  
  53.         if( count( $children ) == 0 ) {
  54.           /* Page has no childs */
  55.  
  56.         } else {
  57.           /* Page has childpages */
  58.           $args = array(
  59.                   'post_type'      => 'page',
  60.                   'posts_per_page' => -1,
  61.                   'post_parent'    => $post->ID,
  62.                   'order'          => 'ASC',
  63.                   'orderby'        => 'menu_order'
  64.            );
  65.  
  66.           $parent = new WP_Query( $args );
  67.           if ( $parent->have_posts() ) : ?>
  68.                 <div class="parent childpages list">
  69.                   <?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
  70.                           <span id="parent-<?php the_ID(); ?>" class="parent-page">
  71.                                   <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
  72.                           </span>
  73.                   <?php endwhile; ?>
  74.                 </div>
  75.           <?php endif; wp_reset_query();
  76.         }
  77.   }
  78. ?>
  79.  
  80.  
  81.  
  82. <?php if($has_ancestors && $enable_back) : ?>
  83. <div class="back">
  84.   <span class="backlink"><a href="<?php echo $ancestor_url; ?>"><i class="fa fa-long-arrow-left" aria-hidden="true"></i> <?php echo get_the_title( $ancestors[0] ) ?></a></span>
  85. </div>
  86. <?php endif; ?>

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.