Test if page is child and has children, if so echo child pages also on grandchild pages

Use ancestors: $ancestors = array_reverse( get_post_ancestors( $post->ID ) ); // reverse ancestors to make it more intuitive if ( isset( $ancestors[0] ) ) { if ( isset( $ancestors[1] ) ) { // 3rd tier $parent_id = $post->post_parent; } else { // 2nd tier $parent_id = $post->ID; } $args = array( ‘depth’ => 1, ‘child_of’ => … Read more

How can I list all pages with their templates?

https://codex.wordpress.org/Function_Reference/get_page_template_slug a basic query to get all pages, sorted by title, then output page title and template file name: $args = array( ‘post_type’ => array( ‘page’ ), ‘order’ => ‘ASC’, ‘orderby’ => ‘title’ ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); echo ‘<p>’; the_title(); echo … Read more

How to List All Pages (With their template names) Within a Website

Not so hard as you expected. Try this: global $wpdb; $sql = “SELECT post_title, meta_value FROM $wpdb->posts a JOIN $wpdb->postmeta b ON a.ID = b.post_id WHERE a.post_type=”page” AND a.post_status=”publish” AND b.meta_key = ‘_wp_page_template’ “; $pages = $wpdb->get_results($sql); echo ‘<pre>’; print_r($pages); echo ‘</pre>’;

Highlighting current item of custom post types’ sub pages, listed by wp_list_pages

The problem with this code is that the query is being altered and current_page_item is being lost. This is the part causing problems. $post = get_post( $getid ); setup_postdata( $post ); If you comment out those two lines you’ll see that current_page_item appears. There are also some secondary problems with this code that have no … Read more

Show just one level of child pages, wp_list_pages woe

This should work, using nothing more than the available argument-array parameters for wp_list_pages(): specifically, depth and child_of. To display one level of hierarchy, for descendant pages of the current page: <?php // Globalize the $post variable; // probably already available in this context, but just in case… global $post; wp_list_pages( array( // Only pages that … Read more