Check if a current page has children, if so display them, if not display all pages on the same level as current page

<div class="container-fuild also-see "> <div class="container"> <div class="row"> <?php function has_children() { global $post; $pages = get_pages( 'child_of=' . $post->ID ); if ( count( $pages ) > 0 ): return true; else: return false; endif; } function is_top_level() { global $post, $wpdb; $current_page = $wpdb->get_var( "SELECT post_parent FROM $wpdb->posts WHERE ID = " . $post->ID ); return $current_page; } // Somewhere in your template $base_args = array( 'hierarchical' => 0 ); if ( has_children() ): $args = array( 'child_of' => $post->ID, 'parent' => $post->ID ); else: if ( is_top_level() ): $args = array( 'parent' => $post->post_parent ); else: $args = array( 'parent' => 0 ); endif; endif; $args = array_merge( $base_args, $args ); $pages = get_pages( $args ); foreach ( $pages as $page ): $image = get_the_post_thumbnail( $page->ID); if($c_page !== $page->post_title) { echo '<div class="col-xl-3 col-lg-3 animated wow fadeIn"> <div class="image-with-title"> <a href="' . get_permalink( $page->ID ) . '"> ' . $image . ' <div class="row title">' . $page->post_title . '</div> </a> </div> </div>'; } //echo '<li><a href="' . get_permalink( $page->ID ) . '">' . $page->post_title . '</a></li>'; endforeach; ?> </div> </div> </div>
Thanks to Lukasz Klis
( https://gist.github.com/lukaszklis)

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.