I have created a column in my functions.php
file to add in a column to see the featured image on posts. However i would like to do this for pages as well, but if i repeat the code replacing posts with pages it creates an error on the site. Does anyone know how i could do this for both with one bit of code?
This is the code i have for posts:
add_filter('manage_posts_columns', 'add_img_column');
add_filter('manage_posts_custom_column', 'manage_img_column', 10, 2);
function add_img_column($columns) {
$columns = array_slice($columns, 0, 1, true) + array("links" => "Image") + array_slice($columns, 1, count($columns) - 1, true);
return $columns;
}
function manage_img_column($column_name, $post_id) {
if( $column_name == 'links' ) {
echo get_the_post_thumbnail($post_id, 'thumbnail');
}
return $column_name;
}
But if i duplicate the code changing 'posts' for 'pages' on the second one, this gives me errors:
add_filter('manage_posts_columns', 'add_img_column');
add_filter('manage_posts_custom_column', 'manage_img_column', 10, 2);
function add_img_column($columns) {
$columns = array_slice($columns, 0, 1, true) + array("links" => "Image") + array_slice($columns, 1, count($columns) - 1, true);
return $columns;
}
function manage_img_column($column_name, $post_id) {
if( $column_name == 'links' ) {
echo get_the_post_thumbnail($post_id, 'thumbnail');
}
return $column_name;
}
add_filter('manage_pages_columns', 'add_img_column');
add_filter('manage_pages_custom_column', 'manage_img_column', 10, 2);
function add_img_column($columns) {
$columns = array_slice($columns, 0, 1, true) + array("links" => "Image") + array_slice($columns, 1, count($columns) - 1, true);
return $columns;
}
function manage_img_column($column_name, $post_id) {
if( $column_name == 'links' ) {
echo get_the_post_thumbnail($post_id, 'thumbnail');
}
return $column_name;
}
Sorry if this is really obvious answer, but I'm really new to wordpress and php so any help explaining the reason why you gave the answer would be greatly appreciated! Thanks.
I have created a column in my functions.php
file to add in a column to see the featured image on posts. However i would like to do this for pages as well, but if i repeat the code replacing posts with pages it creates an error on the site. Does anyone know how i could do this for both with one bit of code?
This is the code i have for posts:
add_filter('manage_posts_columns', 'add_img_column');
add_filter('manage_posts_custom_column', 'manage_img_column', 10, 2);
function add_img_column($columns) {
$columns = array_slice($columns, 0, 1, true) + array("links" => "Image") + array_slice($columns, 1, count($columns) - 1, true);
return $columns;
}
function manage_img_column($column_name, $post_id) {
if( $column_name == 'links' ) {
echo get_the_post_thumbnail($post_id, 'thumbnail');
}
return $column_name;
}
But if i duplicate the code changing 'posts' for 'pages' on the second one, this gives me errors:
add_filter('manage_posts_columns', 'add_img_column');
add_filter('manage_posts_custom_column', 'manage_img_column', 10, 2);
function add_img_column($columns) {
$columns = array_slice($columns, 0, 1, true) + array("links" => "Image") + array_slice($columns, 1, count($columns) - 1, true);
return $columns;
}
function manage_img_column($column_name, $post_id) {
if( $column_name == 'links' ) {
echo get_the_post_thumbnail($post_id, 'thumbnail');
}
return $column_name;
}
add_filter('manage_pages_columns', 'add_img_column');
add_filter('manage_pages_custom_column', 'manage_img_column', 10, 2);
function add_img_column($columns) {
$columns = array_slice($columns, 0, 1, true) + array("links" => "Image") + array_slice($columns, 1, count($columns) - 1, true);
return $columns;
}
function manage_img_column($column_name, $post_id) {
if( $column_name == 'links' ) {
echo get_the_post_thumbnail($post_id, 'thumbnail');
}
return $column_name;
}
Sorry if this is really obvious answer, but I'm really new to wordpress and php so any help explaining the reason why you gave the answer would be greatly appreciated! Thanks.
The reason you're getting an error is because you've got multiple functions with the same name. You have two functions called add_img_column()
and two called manage_img_column()
. You can't have two functions with the same name in PHP.
You don't even need two functions though. You can hook the same function into multiple hooks. So this is all you need:
add_filter('manage_posts_columns', 'add_img_column');
add_filter('manage_pages_columns', 'add_img_column');
add_filter('manage_posts_custom_column', 'manage_img_column', 10, 2);
add_filter('manage_pages_custom_column', 'manage_img_column', 10, 2);
function add_img_column($columns) {
$columns = array_slice($columns, 0, 1, true) + array("links" => "Image") + array_slice($columns, 1, count($columns) - 1, true);
return $columns;
}
function manage_img_column($column_name, $post_id) {
if( $column_name == 'links' ) {
echo get_the_post_thumbnail($post_id, 'thumbnail');
}
return $column_name;
}