Please can someone help me add an additional column to the Manage Pages screen. The column I would like to add would show which template had been applied to each page.
I have hunted for an answer online and cobbled together the below code - NOTE: the code obviously doesn't work, but hopefully gives an idea of what I am trying to achieve.
Thank you
function add_page_text_column($page_columns) {
$page_columns['page_template'] = "Page template";
return $page_columns;
}
add_action('manage_pages_columns', 'add_page_text_column');
function show_page_template_column($post){
global $post;
switch ($post) {
case 'column-page-template':
$page_template = get_post_meta($post_id, '_wp_page_template', true); // file name
$result = array_search($page_template, get_page_templates()); // get template nice name
echo $result;
break;
default:
break;
}
}
add_action('manage_pages_custom_column','show_page_template_column');
Navjot Singh answers:
There is a plugin to do the job already. Here it is: [[LINK href="http://wordpress.org/extend/plugins/reveal-page-templates/"]]Reveal Page Templates[[/LINK]]
designbuildtest comments:
Thanks Navjot. This plugin looks good, but I was hoping for a simple function to put in my functions.php file.
Navjot Singh comments:
Okay. Try this then?
function add_page_text_column($page_columns) {
$page_columns['page_template'] = "Page template";
return $page_columns;
}
add_action('manage_pages_columns', 'add_page_text_column');
function show_page_template_column($column_name, $post_id){
if( $column_name == 'page_template' ) {
$page_template = get_post_meta($post_id, '_wp_page_template', true); // file name
$templates = get_page_templates();
$result = array_search($page_template, $templates); // get template nice name
if( !$result )
echo '<em>'.__('default').'</em>';
echo $result;
}
}
add_action('manage_pages_custom_column','show_page_template_column',10,2);
designbuildtest comments:
Works great Navjot - thanks.
Is there a way to easily set the width of the new Page template column? I have unset the author, comment and date columns and would like to right-align the Page template column with the right-hand edge of the screen.
Navjot Singh comments:
Try using .column-{name} class to set width of your custom column.
.column-page_template { width:20%; }
should work.
designbuildtest comments:
Works great. Thanks.
Arnav Joy answers:
try this
function add_page_text_column($page_columns) {
$page_columns['page_template'] = "Page template";
return $page_columns;
}
add_action('manage_pages_columns', 'add_page_text_column');
function show_page_template_column($column_name, $post_id ){
switch ($column_name) {
case 'page_template':
$page_template = get_post_meta($post_id, '_wp_page_template', true); // file name
$result = array_search($page_template, get_page_templates()); // get template nice name
echo $result;
break;
default:
break;
}
}
add_action('manage_pages_custom_column','show_page_template_column',10,2);
designbuildtest comments:
Works perfectly. Thanks Arnav. Please could you update your code so a default message is displayed if no page template has been defined. Thanks.
Romel Apuya answers:
try this
function add_page_text_column($page_columns) {
$page_columns['page_template'] = "Page template";
return $page_columns;
}
add_action('manage_pages_columns', 'add_page_text_column');
function show_page_template_column($post){
global $post;
switch ($post) {
case 'column-page-template':
$page_template = get_post_meta($post->ID, '_wp_page_template', true); // file name
$result = $page_template; // get template nice name
var_dump($result);
echo $result;
break;
default:
break;
}
}
add_action('manage_pages_custom_column','show_page_template_column');
designbuildtest comments:
Doesn't work sorry.