Home  >  Article  >  Backend Development  >  Column display in custom WordPress post and custom post type admin screens

Column display in custom WordPress post and custom post type admin screens

WBOY
WBOYOriginal
2023-09-17 17:13:011128browse

In this tutorial, we will learn how to add a new column to the WordPress post admin screen, in this column we will display the featured image for each post. This new column will also be added to the admin screen for any active custom post types.

step 1. Activate featured images

In this tutorial, we will use the functions.php file located in the active theme directory. If the file does not exist, you can create a new file with the following content:

<?php
// FUNCTIONS
?>

First, check if the featured image is available on the Add New Post page:

自定义 WordPress 帖子和自定义帖子类型管理屏幕中的列显示

If you don't see the Featured Image box, add this line to functions.php:

add_theme_support('post-thumbnails');

add_theme_support() Function registers support for specified functions within the theme. The argument passed is a string specifying the functionality to be added. In this case, it's post-thumbnails. Other values ​​can be widgets, featured content, menus, etc.

We also set a custom size of 200 pixels for showing the preview of the featured image:

add_image_size('featured_preview', 200, 150, false);

add_image_size()The function registers a new image size for us, with a width of 200px and a height of 150px. We also tell WordPress to use resizing to achieve the desired size. However, you can use clipping by setting the fourth parameter to true.


Step 2. Add custom columns to posts screen

Now we will add a new column to the posts list which will contain the featured image for each post. But first, we need a function called ST4_get_featured_image() to get the post’s featured image.

Open the functions.php file in the theme directory and add the following content:

// GET FEATURED IMAGE
function ST4_get_featured_image($post_ID) {
    $post_thumbnail_id = get_post_thumbnail_id($post_ID);
	if ($post_thumbnail_id) {
		$post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, 'featured_preview');
		return $post_thumbnail_img[0];
	}
}

We use the get_post_thumbnail_id() function to get the post thumbnail ID. The term post thumbnail here refers to the featured image.

We have defined two functions: the first will add the new column and the second will call and display the featured image in each cell of the new column:

// ADD NEW COLUMN
function ST4_columns_head($defaults) {
    $defaults['featured_image'] = 'Featured Image';
	return $defaults;
}
// SHOW THE FEATURED IMAGE
function ST4_columns_content($column_name, $post_ID) {
	if ($column_name == 'featured_image') {
		$post_featured_image = ST4_get_featured_image($post_ID);
		if ($post_featured_image) {
			echo '<img src="' . $post_featured_image . '" />';
		}
	}
}

These two functions will be "hooked" into the WordPress core functions that create the Posts table.

About WordPress Hooks

Developers can modify WordPress default behavior through the WordPress API:

WordPress provides hooks that allow your plugin to "hook" into the rest of WordPress; that is, call a function in the plugin at a specific time, thereby starting the plugin.

In short, Hooks allow developers to extend WordPress functionality without editing core files. There are two types of Hooks: Actions and Filters. Both are launched during WordPress execution, but the filter accepts, transforms and returns the input , whereas the action returns nothing but prints everything you need.

In our example, the ST4_columns_head() function takes the $defaults array, which contains the default Posts table columns (title, category, tag, etc...), to Adds a new featured_image item to the array and returns it to the WordPress core function for printing table HTML.

In contrast, the ST4_columns_content() function accepts two variables ($column_name and $post_ID) and prints the output based on them . More precisely, ST4_columns_content() is called on every iteration of the loop that iterates over the $defaults array. On each iteration, WordPress passes two parameters to our function: the column name and the post ID. This function analyzes all column names and checks for the featured image when the name is equal to the name we specified in ST4_columns_head().

So, now we can hook our function into the WordPress plugin API.

We will use the

manage_posts_columns filter to hook into the ST4_columns_head() function. This filter can help us modify the columns displayed in the Posts list.

Another hook we use is

manage_posts_custom_column which will trigger the hook function for each custom column in the post list. Our ST4_columns_content() function checks if the column name matches the column name we want to output the content for.

add_filter('manage_posts_columns', 'ST4_columns_head');
add_action('manage_posts_custom_column', 'ST4_columns_content', 10, 2);
The

10 and 2 parameters are: the order of function execution (priority) and the number of parameters accepted by the function. Anyway, you can read more in the WordPress Codex under add_action.


Final Results

Now we can finally write a post with a featured image:

自定义 WordPress 帖子和自定义帖子类型管理屏幕中的列显示

因此,当您在 /wp-admin/edit.php 中打开管理帖子屏幕时,您将看到新的特色图片列:

自定义 WordPress 帖子和自定义帖子类型管理屏幕中的列显示

前两个帖子有特色图片,第三个帖子(没有特色图片的帖子)没有,因此不会显示任何内容。

要为没有特色图像的帖子显示默认图像,您可以这样修改 ST4_columns_content() 函数:

<?php
function ST4_columns_content($column_name, $post_ID) {
    if ($column_name == 'featured_image') {
		$post_featured_image = ST4_get_featured_image($post_ID);
		if ($post_featured_image) {
			// HAS A FEATURED IMAGE
			echo '<img src="' . $post_featured_image . '" />';
		}
		else {
			// NO FEATURED IMAGE, SHOW THE DEFAULT ONE
			echo '<img src="' . get_bloginfo( 'template_url' ); . '/images/default.jpg" />';
		}
	}
}
?>

default.jpg 图像必须存在于我们活动主题的 images 目录中。

自定义 WordPress 帖子和自定义帖子类型管理屏幕中的列显示

您还可以通过打开屏幕选项面板并单击特色图像复选框来显示/隐藏此新列:

自定义 WordPress 帖子和自定义帖子类型管理屏幕中的列显示

第 3 步。 将特色图像列添加到自定义帖子类型

WordPress 最有趣和最有用的功能之一是可以添加自定义帖子类型(以及自定义分类法)。您可以使用帖子类型创建不同于帖子和页面的新内容类型,例如管理电影数据库。事实上,当您添加自定义帖子类型时,WordPress 会创建该帖子类型的所有管理页面:您可以像默认帖子和页面一样添加、编辑和删除这些帖子。

现在我们通过 WordPress register_post_type() 函数创建一个新的自定义帖子类型:电影

add_action('init', 'ST4_add_movies');  
function ST4_add_movies() {
    $args = array(
		'label' => __('Movies'),
		'singular_label' => __('Movie'),
		'public' => true,
		'show_ui' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'rewrite' => true,
		'supports' => array('title', 'editor', 'thumbnail')
	);
	register_post_type( 'movie' , $args );
}

此功能可让您注册新的帖子类型。您应该注意,我们正在使用 init 挂钩来调用 ST4_add_movies(),然后它会注册我们的 movie 帖子类型。我们传递给 register_post_type() 函数的参数数组决定了自定义帖子类型的行为方式。在本例中,我们将 show_ui 键的值设置为 true。这会生成通过管理仪表板管理电影帖子类型所需的 UI。

因此,当您将特色图像添加到电影帖子时...

自定义 WordPress 帖子和自定义帖子类型管理屏幕中的列显示

...您还将在管理电影屏幕中看到特色图像(请注意,您还可以在此处显示/隐藏“屏幕选项”中的列):

自定义 WordPress 帖子和自定义帖子类型管理屏幕中的列显示

其他用法

根据帖子类型添加自定义列

如果您使用 manage_posts_columnsmanage_posts_custom_column 挂钩,该列将显示在所有管理帖子屏幕中。事实上,这些过滤器适用于除页面之外的所有类型的帖子

// ALL POST TYPES: posts AND custom post types
add_filter('manage_posts_columns', 'ST4_columns_head');
add_action('manage_posts_custom_column', 'ST4_columns_content', 10, 2);

如果您想仅在管理帖子屏幕中添加列,请使用:

// ONLY WORDPRESS DEFAULT POSTS
add_filter('manage_post_posts_columns', 'ST4_columns_head', 10);
add_action('manage_post_posts_custom_column', 'ST4_columns_content', 10, 2);

如果您想仅在管理页面屏幕中添加列,请使用:

// ONLY WORDPRESS DEFAULT PAGES
add_filter('manage_page_posts_columns', 'ST4_columns_head', 10);
add_action('manage_page_posts_custom_column', 'ST4_columns_content', 10, 2);

如果您想仅在管理电影屏幕中添加列,请使用:

// ONLY MOVIE CUSTOM TYPE POSTS
add_filter('manage_movie_posts_columns', 'ST4_columns_head_only_movies', 10);
add_action('manage_movie_posts_custom_column', 'ST4_columns_content_only_movies', 10, 2);

// CREATE TWO FUNCTIONS TO HANDLE THE COLUMN
function ST4_columns_head_only_movies($defaults) {
    $defaults['directors_name'] = 'Director';
	return $defaults;
}

function ST4_columns_content_only_movies($column_name, $post_ID) {
	if ($column_name == 'directors_name') {
		// show content of 'directors_name' column
	}
}
 

将自定义列添加到其他帖子类型

如果您有其他自定义帖子类型,您可以轻松向其中添加特色图像列。如果您手动添加帖子类型,请检查添加自定义帖子的文件并查找 register_post_type() 函数的第一个参数:

register_post_type( 'book' , $args ); // book is the post type

如果自定义帖子类型是通过其他插件定义的并且/或者您找不到 register_post_type() 的位置,请在浏览器中打开自定义帖子管理屏幕并检查 URL:

http://www.yoursite.com/wp-admin/edit.php?post_type=<u>book</u>

在本例中,图书是帖子类型。

最后,以这种方式修改钩子,将电影替换为书籍

add_filter('manage_book_posts_columns', 'ST4_columns_book_head');
add_action('manage_book_posts_custom_column', 'ST4_columns_book_content', 10, 2);

不要忘记创建两个函数:ST4_columns_book_head() 用于创建列,ST4_columns_book_content() 用于显示列内容。

添加两个自定义列

如果您需要添加多个列,您可以轻松执行以下操作:

// ADD TWO NEW COLUMNS
function ST4_columns_head($defaults) {
    $defaults['first_column']  = 'First Column';
    $defaults['second_column'] = 'Second Column';
    return $defaults;
}

function ST4_columns_content($column_name, $post_ID) {
    if ($column_name == 'first_column') {
        // First column
    }
    if ($column_name == 'second_column') {
        // Second column
    }
}

删除默认列

您还可以删除 WordPress 默认列,例如帖子管理屏幕中的类别列:


add_filter('manage_post_posts_columns', 'ST4_columns_remove_category');

// REMOVE DEFAULT CATEGORY COLUMN
function ST4_columns_remove_category($defaults) {
    // to get defaults column names:
    // print_r($defaults);
    unset($defaults['categories']);
    return $defaults;
}

参考文献

  • WordPress 插件 API
  • WordPress 自定义帖子类型
  • 函数参考/添加操作
  • 插件 API/过滤器参考/管理帖子列
  • 插件 API/操作参考/管理帖子自定义列
  • 插件 API/过滤器参考/管理 $post 类型帖子列
  • 插件 API/操作参考/管理 $post 类型帖子自定义列

The above is the detailed content of Column display in custom WordPress post and custom post type admin screens. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn