wordpress 自定义
In my previous post, I introduced Custom Post Types (CPT) and how to create one for your WordPress powered website.
在上一篇文章中 ,我介绍了自定义帖子类型(CPT)以及如何为您的WordPress支持的网站创建一个。
We also took a look at how to customize the various UI labels of a custom post type to make it distinct from the native post and page post types. However, we didn’t cover how to customize the admin notices generated by them.
我们还研究了如何自定义自定义帖子类型的各种UI标签,以使其与本机帖子和页面帖子类型不同。 但是,我们没有介绍如何自定义由它们生成的管理通知。
In this tutorial, I will be covering how to customize these notices and also how to register new taxonomies to a custom post type.
在本教程中,我将介绍如何自定义这些通知,以及如何将新分类法注册为自定义帖子类型。
自定义CPT管理员通知 (Customizing CPT Admin Notices)
Are you familiar with the alert message that is displayed near the top of admin pages for example when a post is saved as draft, published or even when you save the settings of a plugin? This message is what is referred to as an admin notice.
您是否熟悉管理页面顶部附近显示的警报消息,例如,将帖子另存为草稿,发布或什至在保存插件设置时? 此消息是所谓的管理员通知 。
By default, the admin notices displayed when working on a custom post assumes you are dealing with a post
post type and therefore, when for example a book
post type is updated, the following notice is displayed: Post updated. View post
.
默认情况下,管理员通知上的自定义后的工作假设你正在处理一个,当显示post
交型,因此,当例如book
:Struts式更新时,会显示以下通知Post updated. View post
Post updated. View post
。
You can change the text of these messages easily by using the post_updated_messages
hook like so:
您可以使用post_updated_messages
钩子轻松更改这些消息的文本,如下所示:
add_filter( 'post_updated_messages', 'book_cpt_messages' );
/**
* Book CPT updates messages.
*
* @param array $messages Existing post update messages.
*
* @return array Amended book CPT notices
*/
function book_cpt_messages( $messages ) {
$post = get_post();
$post_type = get_post_type( $post );
$post_type_object = get_post_type_object( $post_type );
$messages['book'] = array(
0 => '', // Unused. Messages start at index 1.
1 => __( 'Book updated.', 'textdomain' ),
2 => __( 'Custom field updated.', 'textdomain' ),
3 => __( 'Custom field deleted.', 'textdomain' ),
4 => __( 'Book updated.', 'textdomain' ),
5 => isset( $_GET['revision'] ) ? sprintf( __( 'Book restored to revision from %s', 'textdomain' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => __( 'Book published.', 'textdomain' ),
7 => __( 'Book saved.', 'textdomain' ),
8 => __( 'Book submitted.', 'textdomain' ),
9 => sprintf(
__( 'Book scheduled for: <strong>%1$s</strong>.', 'textdomain' ),
date_i18n( __( 'M j, Y @ G:i', 'textdomain' ), strtotime( $post->post_date ) )
),
10 => __( 'Book draft updated.', 'textdomain' )
);
if ( $post_type_object->publicly_queryable ) {
$permalink = get_permalink( $post->ID );
$view_link = sprintf( ' <a href="%s">%s</a>', esc_url( $permalink ), __( 'View book', 'textdomain' ) );
$messages[ $post_type ][1] .= $view_link;
$messages[ $post_type ][6] .= $view_link;
$messages[ $post_type ][9] .= $view_link;
$preview_permalink = add_query_arg( 'preview', 'true', $permalink );
$preview_link = sprintf( ' <a target="_blank" href="%s">%s</a>', esc_url( $preview_permalink ), __( 'Preview book', 'textdomain' ) );
$messages[ $post_type ][8] .= $preview_link;
$messages[ $post_type ][10] .= $preview_link;
}
return $messages;
}
Code Explanation: The code above customizes admin notices generated by a book
custom post type.
代码说明:上面的代码自定义由book
自定义帖子类型生成的管理员通知。
The $messages
multi-dimensional array controls the admin notices displayed by any post type.
$messages
多维数组控制任何帖子类型显示的管理员通知。
To customize the messages of a book
custom post type, create an index array containing the various messages as the value of $messages['book']
.
要自定义book
自定义帖子类型的消息,请创建一个索引数组,其中包含各种消息,作为$messages['book']
。
The if
statement checks if the custom post type is publicly query-able. That is, whether the public
argument is set to true
while registering the custom post type.
if
语句检查自定义帖子类型是否可公开查询。 也就是说,在注册自定义帖子类型时是否将public
参数设置为true
。
If true, a link to view a post belonging to the CPT is added to the admin notice displayed when it is updated, published or scheduled for publication while a link to preview the post is added when it is submitted for review or a draft is updated.
如果为true,则在更新,发布或计划发布时,将显示查看属于CPT的帖子的链接添加到管理通知中,而在将其提交以供审阅或更新草稿时,将添加预览该帖子的链接。 。
自定义分类法 (Custom Taxonomies)
In WordPress, a taxonomy is mechanism for grouping posts of any type.
在WordPress中,分类法是对任何类型的帖子进行分组的机制。
Examples of taxonomies include Category
for grouping posts that are related to a given category and Tag
which is pretty similar to categories but is more free form. More information on taxonomies is available over at the WordPress Codex.
分类的例子包括Category
,用于分组与给定类别和职位Tag
是很相似的类别,但更自由的形式。 有关分类法的更多信息, 请访问WordPress Codex 。
That being said, we are going to cover how to create custom taxonomies. Let us take the example of creating a book
post type, categorizing the book entries using the same categories used for blog posts isn’t ideal.
话虽如此,我们将介绍如何创建自定义分类法。 让我们以创建book
帖类型为例,使用与博客文章相同的类别对书帖进行分类并不理想。
A real life example is the Easy Digital Downloads plugin that uses a download
custom post type for digital product entries with a download_category
taxonomy for product categorization.
一个实际的例子是Easy Digital Downloads插件,该插件将download
自定义帖子类型用于数字产品条目,并使用download_category
分类法对产品进行分类。
To create a custom taxonomy, use the register_taxonomy()
function and hook it to the init
Action like so:
要创建自定义分类法,请使用register_taxonomy()
函数并将其挂钩到init
Action上,如下所示:
add_action( 'init', 'book_category_taxonomy' );
function book_category_taxonomy() {
register_taxonomy(
'book_category',
'book',
array(
'label' => __( 'Book Categories' ),
'rewrite' => array( 'slug' => 'book_category' ),
'hierarchical' => true,
)
);
}
If you have a book
custom post type already, you should see the category taxonomy added to the admin menu and post edit screen.
如果您已经拥有一book
自定义帖子类型,则应该看到类别分类法已添加到管理菜单和帖子编辑屏幕中。
You can also use register_post_type()
for registering custom post types, the register_taxonomy()
function also accepts an array of arguments for customizing labels and configuration of a custom taxonomy.
您还可以使用register_post_type()
来注册自定义帖子类型, register_taxonomy()
函数还接受用于自定义标签和自定义分类标准配置的参数数组。
I won’t be explaining the arguments because they are pretty much the same as that of register_post_type()
. A list of the arguments and descriptions is available here.
我不会解释这些参数,因为它们与register_post_type()
几乎相同。 此处提供了参数和说明的列表。
结论 (Conclusion)
Custom Post Types are a powerful feature of WordPress and useful in grouping data or post entries that don’t fit into a post
and page
type. The icing on the cake is the ability to further categorize the posts of a custom post type by registering a custom taxonomy.
自定义帖子类型是WordPress的一项强大功能,可用于对不适合post
和page
类型的数据或帖子条目进行分组。 锦上添花的是,通过注册自定义分类法,可以进一步对自定义帖子类型的帖子进行分类。
Do you have a question or contribution? Please use the comments to let us know.
您有任何疑问或贡献吗? 请使用评论让我们知道。
翻译自: https://www.sitepoint.com/wordpress-custom-post-types-notices-taxonomies/
wordpress 自定义