3

I want to list all site pages with the template they use.

Is there a mod to wp_list_pages which does this?

7
  • I would note that modifying the source of wp_list_pages or any part of WP Core is bad practice. This could be done using WP_Query, but a little context as to why you want to do this and what you hope to achieve by getting this would be useful. Of note, the Query Monitor plugin will list all templates used. Listing any sub-templates pulled in via get_template_part calls will be a much harder thing to do and require each page be loaded
    – Tom J Nowell
    Nov 3, 2016 at 13:51
  • @Tom J Nowell... Poor wording from me... I'm not considering modding a core file, but whether there's something which can be set in $args. My reason for wanting a front-end page which shows the pages and the main template file (not the sub-templates) they use is so that if I decide to modify/delete a template file I'm aware which pages are affected (and so avoid the scenario of having a page revert to a default template).
    – glvr
    Nov 3, 2016 at 16:42
  • Another thing to note is that which template is loaded isn't just page.php or the various custom page templates, there's also page-$slug.php and page-$id.php
    – Tom J Nowell
    Nov 3, 2016 at 17:24
  • @Tom J Nowell Thanks. I'm trying, but failing, to understand. Sorry. My thinking is... 'Ok, I have a page at /admin/ which uses admin.php as a template, so I'm trying to get something like 'Admin: admin.php' (and likewise for other pages)'.
    – glvr
    Nov 3, 2016 at 20:17
  • I see, so Michaels answer is what you want, but keep in mind that if I have a page named 'hello', WordPress will try to load page-hello.php, and if that fails it will go for page.php etc, look up the template hierarchy for the full list and explanation of how WordPress loads templates
    – Tom J Nowell
    Nov 4, 2016 at 16:13

5 Answers 5

2

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 ' - ';
        echo get_page_template_slug(); 
        echo '</p>';
    }
    wp_reset_postdata();
}
3
  • Thanks. I don't know why, but this only lists 5 pages.
    – glvr
    Nov 7, 2016 at 7:42
  • @gulliver queries default to the posts per page in the settings screen, adding a "posts_per_page" => -1 will show all of them, but I'd advise against that and instead set a high number such as 100 or 200 to prevent problems
    – Tom J Nowell
    Nov 7, 2016 at 14:13
  • 1
    @Tom J Nowell... Ah, thanks. (I hadn't seen your comment, hence my year-late response.)
    – glvr
    Nov 27, 2017 at 12:32
1

If you wish to get a list of posts within all post types that are using a template, use:

$args = array(
        'post_type' => 'any',
        'order' => 'ASC',
        'orderby' => 'title',
        'posts_per_page' => -1
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            if (get_page_template_slug($post->ID)) {
                echo '<a href="' . get_the_permalink() . '">' . get_the_title() . ' - ' . esc_html(get_page_template_slug($post->ID)) . '</a><br>';
            }
        }
        wp_reset_postdata();
    }

Can be a large query though.

0

I have tried flowing code to get all page with template, and worked perfect for me. this code purpose was to get all custom page template,as i was developed single page template.

$args = array(
    "post_type" => "page",
    "order" => "ASC",
    "orderby" => "menu_order"
);
$the_query = new WP_Query( $args );
if ( have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        ?>
        <div id="post-<?php the_ID() ?>">
        <?php
        global $post;
        $slug = $post->post_name;
        get_template_part("page", $slug);
        ?>
        </div>
        <?php
    }
    wp_reset_postdata();
}
2
  • Thanks. Still a novice at this, am I right to assume this code is instead of using wp_list_pages? If so, don't know how to use it, because just wrapping it in open/close php tags delivers an error of 'unexpected <'.
    – glvr
    Nov 3, 2016 at 16:55
  • I've fixed some of the syntax errors in this, but this won't display the page template used, it just uses get_template_part to load a template
    – Tom J Nowell
    Nov 3, 2016 at 17:23
0

Rather than do this in PHP, you can do it with this mysql:

SELECT distinct meta_value FROM `wp_postmeta` WHERE meta_key='_wp_page_template'

I just needed it for a quick check so that I could clean up unused templates with the theme_page_templates filter.

0

Using phpmyadmin (sql) you can run this query to get a list of all pages with its used template:

SELECT p.post_name, p.post_title, meta_key, meta_value FROM `wp_postmeta` INNER JOIN wp_posts p ON p.ID = post_id WHERE meta_key='_wp_page_template' ORDER BY meta_value, p.post_name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.