Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A WordPress page template to identify what page templates are being used where.
<?php
/*
Template Name: Page Template Usage
Description: A page template to identify what page templates are being used where. BRAAAM
*/
get_header();
// Array of page template name => template filename
$templates = array(
'Template Filename' => 'template-filename.php',
);
echo print_page_links_by_template( $templates );
get_footer();
/**
* Returns array of post objects where the specified $template is used.
*
* @param string $template The template file name.
* @param array $args Array of additional args accepted by get_pages.
*
* @return array Array of post objects matching the query.
*/
function get_pages_by_template( $template = '', $args = array() ) {
if ( empty( $template ) ) {
return false;
}
if ( strpos( $template, '.php' ) !== ( strlen( $template ) - 4 ) ) {
$template .= '.php';
}
$args['hierarchical'] = 0;
$args['meta_key'] = '_wp_page_template';
$args['meta_value'] = $template;
$pages = get_pages( $args );
return $pages;
}
/**
* Returns links to the pages by the template used.
*
* @param array $templates Array of templates.
*
* @return string HTML markup of Template Name and pages where used with edit link.
*/
function print_page_links_by_template( $templates ) {
$page_list = '';
if ( $templates ) {
foreach ( $templates as $template_name => $template_file ) {
$page_list .= '<h3>' . $template_name . ' - ' . $template_file . '</h3>';
$pages = get_pages_by_template( $template_file );
if ( $pages ) {
foreach ( $pages as $page ) {
$page_list .= '<a href="' . get_permalink( $page->ID ) . '">' . $page->post_title . '</a> <a href="' . admin_url( 'post.php?post=' ) . $page->ID . '&action=edit" style="font-size: 85%;">(Edit)</a><br />';
}
}
}
}
return $page_list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.