Last active
September 7, 2019 15:56
-
-
Save davebonds/b802ad9fb6cba140d38f2b23a7969f9b to your computer and use it in GitHub Desktop.
A WordPress page template to identify what page templates are being used where.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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