该扩展程序未列在

tech2022-12-25  121

该扩展程序未列在

This tutorial will outline how to extend the WordPress administration screens for your post types by customizing what columns of information are displayed.

本教程将概述如何通过自定义显示哪些信息列来扩展WordPress类型的管理屏幕。

We will start by outlining how WordPress displays your post type listings by default and what information is shown to your users. We will then move onto what hooks will be required and how we will manipulate the columns that are show for your post type.

我们将首先概述WordPress如何默认显示您的帖子类型列表以及向您的用户显示哪些信息。 然后,我们将转到需要哪些挂钩以及如何处理为您的帖子类型显示的列。

Finally we will pull in additional meta data for our posts and display that information in our columns.

最后,我们将为帖子获取其他元数据,并在我们的列中显示该信息。

By the end you should be able to extend and modify the administration screens for your post type, allowing you to show additional information and actions for your users.

到最后,您应该能够扩展和修改帖子类型的管理屏幕,从而可以为用户显示其他信息和操作。

WordPress帖子管理界面 (The WordPress Posts Management Interface)

When you create your own post types (or use the default post types such as ‘posts’ and ‘pages’), WordPress will create an administrative area that you can manage your posts from. This area is generally accessed from the backend of your website via the main administration menu.

当您创建自己的帖子类型(或使用默认的帖子类型,例如“帖子”和“页面”)时,WordPress将创建一个管理区域,您可以从中管理帖子。 通常从网站的后端通过主管理菜单访问该区域。

In the image above, we can either select the ‘Posts’ administration menu or its sub-menu ‘All Posts’.

在上图中,我们可以选择“帖子”管理菜单或其子菜单“所有帖子”。

Selecting either one of these options will bring you to the posts administration screen shown below.

选择这些选项之一将带您进入如下所示的帖子管理屏幕。

This administration screen will outline all of the posts for your post type. In this example it will display all of your blog posts, but it could easily display your ‘pages’ or any custom post types you have defined.

该管理屏幕将概述您的帖子类型的所有帖子。 在此示例中,它将显示您的所有博客文章,但可以轻松显示您的“页面”或您定义的任何自定义文章类型。

扩展帖子管理界面 (Extending the Posts Management Interface)

Now that you know what the posts management is, we can now work on enhancing this interface to provide additional functionality.

现在您知道职位管理是什么,我们现在可以增强此界面以提供其他功能。

The main reason you will want to extend these interfaces is to provide additional actions and information to your end users.

您想要扩展这些界面的主要原因是向最终用户提供其他操作和信息。

If you have expanded the default WordPress post types to include additional meta information (or you have defined entirely new post types), often you will want to show this information to the user directly from this management area.

如果您已经扩展了默认的WordPress帖子类型以包括其他元信息(或者您定义了全新的帖子类型),通常您将希望直接从此管理区域向用户显示此信息。

We are going to show you how you can enhance these post management interfaces by customizing and populating post columns.

我们将向您展示如何通过自定义和填充帖子列来增强这些帖子管理界面。

自定义和填充后列 (Customizing and Populating Post Columns)

When WordPress displays the administrative area for your post type it will list a series of default columns. For example, when viewing Pages you will typically see the Title, Author, Comments and Date as shown below

当WordPress显示您的帖子类型的管理区域时,它将列出一系列默认列。 例如,在查看Pages时,通常会看到Title , Author , Comments和Date ,如下所示

To customize the way in information is displayed you will need to use two filters, one to determine the columns that will be shown for your post type and another to populate your column data.

要自定义信息的显示方式,您将需要使用两个过滤器,一个过滤器确定要针对您的帖子类型显示的列,另一个过滤器以填充您的列数据。

自定义帖子列 (Customizing Post Columns)

To customize your content type you will need to use the manage_$post_type_posts_columns filter.

要自定义内容类型,您将需要使用manage_$post_type_posts_columns过滤器。

Replace $post_type with the name of your content type and you will be able to determine what columns are shown for that type. For example here are a few ways in which this filter can be called

用内容类型的名称替换$post_type ,您将能够确定该类型显示的列。 例如,这里有几种方法可以调用此过滤器

manage_post_posts_columns

manage_post_posts_columns

customise post post type

定制post柱式

manage_post_posts_columns

manage_post_posts_columns

manage_page_posts_columns

manage_page_posts_columns

customise your page post type

自定义您的page帖子类型

manage_page_posts_columns

manage_page_posts_columns

manage_services_posts_columns

manage_services_posts_columns

customise a custom content type called services (that you have defined before)

自定义称为services的自定义内容类型(您之前已定义)

manage_services_posts_columns

manage_services_posts_columns

For our example we will be using the manage_page_posts_columns filter to determine what columns are displayed for our page post type.

对于我们的示例,我们将使用manage_page_posts_columns过滤器来确定针对page帖子类型显示哪些列。

This filter takes in a single parameters called $columns which is an associative array of column names and its displayed title. This variable is what you will be interacting with.

该过滤器接受一个称为$ columns的单个参数,该参数是列名称及其显示标题的关联数组。 该变量就是您要与之交互的变量。

Now that you have access to the $columns array you can either add new columns to your post type, remove columns or even alter the position in which they are displayed. For our example we will be removing a few columns and adding our new ones.

现在,您可以访问$ columns数组,可以将新列添加到帖子类型中,删除列,甚至可以更改它们的显示位置。 对于我们的示例,我们将删除一些列并添加新列。

//manage the columns of the `page` post type function manage_columns_for_page($columns){ //remove columns unset($columns['date']); unset($columns['comments']); unset($columns['author']); //add new columns $columns['page_featured_image'] = 'Page Featured Image'; $columns['page_template'] = 'Page Template'; $columns['page_content'] = 'Page Content'; return $columns; } add_action('manage_page_posts_columns','manage_columns_for_page');

This function will first remove the date, comments and author columns using the unset() function (which removes / destroys variables). Secondly, the function will add three new columns called page_featured_image, page_template and page_content.

此函数将首先使用unset()函数(删除/销毁变量)删除date , comments和author列。 其次,该函数将添加三个新列,分别称为page_featured_image , page_template和page_content 。

填充后列 (Populating Post Columns)

Now that you have determined what columns will be shown for your post type, you will need to hook into another filter so you can populate your columns.

既然您已经确定了将为您的帖子类型显示哪些列,那么您将需要连接到另一个过滤器,以便填充列。

Themanage_$post_type_posts_custom_column filter will be used for this purpose.

manage_$post_type_posts_custom_column过滤器将用于此目的。

Replace $post_type with the name of your content type and you will be able to determine exactly what content is displayed in your columns. For example here are a few ways in which this filter can be called

将$post_type替换$post_type您的内容类型的名称,您将可以准确确定列中显示的内容。 例如,这里有几种方法可以调用此过滤器

manage_post_posts_custom_column

manage_post_posts_custom_column

populate the post post type columns

填充post岗位类型列

manage_post_posts_custom_column

manage_post_posts_custom_column

manage_page_posts_custom_column

manage_page_posts_custom_column

populate the page post type columns

填充page帖子类型列

manage_page_posts_custom_column

manage_page_posts_custom_column

manage_services_posts_custom_column

manage_services_posts_custom_column

populate the columns for a custom content type called services

为称为services的自定义内容类型填充列

manage_services_posts_custom_column

manage_services_posts_custom_column

For our tutorial we will be populating the columns for our page post type so we will be calling the manage_page_posts_custom_column filter.

在本教程中,我们将填充page文章类型的列,因此将调用manage_page_posts_custom_column过滤器。

This filter takes in two parameter, $column and $post_id. With these variables we are able to determine what column is being customized (since it will loop through all columns set in the $columns array) and also what the ID of the post is (so we can use WordPress functions given that we know what post we want to pull data from).

该过滤器接受两个参数$column和$post_id 。 使用这些变量,我们能够确定正在自定义的列(因为它将遍历$columns数组中设置的所有列)以及帖子的ID(因此我们可以使用WordPress函数,因为我们知道哪个帖子)我们要从中提取数据)。

We will call a function, passing in those variables and then look for our newly created columns. When we find the column we are looking for we will collect our data and then output it for display.

我们将调用一个函数,传入这些变量,然后查找我们新创建的列。 找到所需的列后,我们将收集数据,然后将其输出以显示。

//Populate custom columns for `page` post type function populate_page_columns($column,$post_id){ //featured image column if($column == 'page_featured_image'){ //if this page has a featured image if(has_post_thumbnail($post_id)){ $page_featured_image = get_the_post_thumbnail($post_id,'thumbnail'); echo $page_featured_image; }else{ echo 'This page has no featured image'; } } //page template column if($column == 'page_template'){ //get the current page template being used for the page $page_template_name = get_post_meta( $post_id, '_wp_page_template', true ); //get a listing of all of the page templates for our site $page_templates = get_page_templates(); if(in_array($page_template_name,$page_templates)){ //search through each template foreach($page_templates as $key <= $value){ //if the template matches our current template, we found it if($page_template_name == $value){ echo 'This page is using the ' . $key . ' template'; } } }else{ echo 'This page is using the default template'; } } //page content column if($column == 'page_content'){ //get the page based on its post_id $page = get_post($post_id); if($page){ //get the main content area $page_content = apply_filters('the_content', $page->post_content); echo $page_content; } } } add_action('manage_page_posts_custom_column','populate_page_columns',10,2);

For our featured image column we are checking to see if the page has a featured image (post thumbnail) set. We call has_post_thumbnail() and pass in our $post_id variable which returns either true or false depending on if a featured image has been set for this page.

对于我们的特色图片列,我们正在检查页面是否设置了特色图片(缩略图)。 我们调用has_post_thumbnail()并传入$post_id变量,该变量返回true或false,具体取决于是否为此页面设置了特色图片。

If we do have a featured image we call the get_the_post_thumbnail() function, passing in our $post_id variable and the size we want thumbnail. We will then echo this image item out and it will appear inside our column.

如果我们有特色图片,我们将调用get_the_post_thumbnail()函数,传入$post_id变量和我们想要的thumbnail大小。 然后,我们将回显此图像项,它将出现在我们的列中。

For our page template column we first get the current page template being used by the page by looking at its _wp_page_template meta data. This key holds the filename of what template will be served when displaying the page.

对于页面模板列,我们首先通过查看其_wp_page_template元数据来获取页面正在使用的当前页面模板。 此键保存显示页面时将提供的模板的文件名。

After this we get an array containing all of the websites page templates with the get_page_templates() function. This returns an associative array where the key is the pretty name of the template (defined in your templates with the phrase template name: name_of_the_template) and its value as the actual file-name of the template (for example my_page_template.php)

之后,我们得到一个包含所有带有get_page_templates()函数的网站页面模板的get_page_templates() 。 这将返回一个关联数组,其中键是模板的漂亮名称(在模板中使用短语template name: name_of_the_template ),其值作为模板的实际文件名(例如my_page_template.php)。

We check to see if the page template for this page exists in the listing of all page templates. If it doesn’t it means that it is using the default template (meaning it will be displaying page.php).

我们检查所有页面模板的列表中是否存在该页面的页面模板。 如果不是,则表示它正在使用default模板(表示将显示page.php)。

If the value does exist however we then loop through all of the registered page templates and compare their values to the current meta value of the page. When we have found a template with the same name, we know this template is correct and we display its pretty name (by echoing out a simple statement)

如果该值确实存在,那么我们将遍历所有已注册的页面模板,并将它们的值与页面的当前元值进行比较。 找到具有相同名称的模板后,我们知道该模板是正确的,并显示其漂亮名称(通过回显一个简单的语句)

For our page content column we call the get_post() function, passing in our $post_id variable. This function will retrieve the page belonging to this ID. If we found a page we then collect the pages content into a variable by using the apply_filters function. We pass into this filter the value of the_contentand also the page content pulled from the $page variable (post object). Once collected we display it.

对于页面内容列,我们调用get_post()函数,并传入$post_id变量。 此函数将检索属于该ID的页面。 如果找到页面,则使用apply_filters函数将页面内容收集到一个变量中。 我们将the_content的值以及从$page变量(post对象)提取的页面内容传递到此过滤器中。 收集后,我们将其显示。

As a final note, notice how we are passing 10 and 2 into our add_action function. These specify that this hook will execute on priority 10 and also it will pass in 2 arguments. Feel free to view the documentation for the add_action function on the WordPress Codex

最后,请注意我们如何将10和2传递到add_action函数中。 这些指定该挂钩将在优先级10上执行,并且还将传入2个参数。 随意查看WordPress Codex上add_action函数的文档

Below is an example of how your page administration screen could given your new columns.

以下是页面管理屏幕如何分配新列的示例。

结论 (In Conclusion)

Now that you know how to extend your pages administration screen with customized columns, you can go through all of your post types and customize the exact data you want shown for your post.

既然您知道如何使用自定义列扩展pages管理屏幕,那么您可以遍历所有帖子类型并自定义想要为帖子显示的确切数据。

Being able to customize what data you display to your users will be highly useful for custom post types as you can bring in whatever information you need given that you have access to each post’s ID (and thus all of its meta data). You may even want to add additional actions into these columns, such as dynamically updating content or selecting new media, this is all possible with customized columns.

能够自定义显示给用户的数据对于自定义帖子类型将非常有用,因为您可以输入需要的任何信息,前提是您有权访问每个帖子的ID(以及所有元数据)。 您甚至可能希望在这些列中添加其他操作,例如动态更新内容或选择新媒体,而使用自定义列则可以实现所有这些操作。

翻译自: https://www.sitepoint.com/extending-post-columns-admin-areas/

该扩展程序未列在

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)