>我们还研究了如何自定义自定义帖子类型的各种UI标签,以使其与本机
post>和 page post类型不同。但是,我们没有介绍如何自定义他们生成的管理员通知。> >在本教程中,我将介绍如何自定义这些通知以及如何向自定义邮政类型注册新的分类法。
自定义CPT Admin通知
默认情况下,在自定义帖子上工作时显示的管理员通知假设您正在处理帖子类型,因此,当更新书籍邮政类型时,显示以下通知:帖子更新。查看帖子。
>您可以使用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; }$消息多维数组控制任何帖子类型显示的管理通知。
>自定义书籍自定义帖子类型的消息,创建一个包含各种消息的索引数组,作为$消息的值['book']。
if语句检查自定义邮政类型是否公开查询。也就是说,在注册自定义邮政类型时,公共论点是否设置为true。
>如果为true,则查看属于CPT的帖子的链接将添加到更新,发布或计划出版时显示的管理通知中,而在提交帖子进行审查或提交帖子时,则添加了该帖子。草稿已更新。
自定义分类法
在WordPress中,分类法是分类任何类型帖子的机制。
>分类法的示例包括与给定类别和标签相关的帖子分组的类别,该帖子与类别非常相似但更自由形式。有关分类法的更多信息,请访问WordPress codex。 话虽如此,我们将介绍如何创建自定义分类法。让我们以创建图书帖子类型的示例,使用与博客文章相同的类别对书籍进行分类。
>>现实生活中的示例是一个简单的数字下载插件,该插件使用下载定制帖子类型用于数字产品条目,其中download_category分类法用于产品分类。
为创建自定义分类法,使用register_taxonomy()函数并将其挂钩到INIT ACTION,例如So:
如果您已经有一本书自定义帖子类型,则应看到添加到管理菜单和发布屏幕的类别分类法。
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; }
>您还可以使用register_post_type()进行登记自定义帖子类型,register_taxonomy()函数还接受了自定义标签和配置自定义分类法的参数数组。
>
结论>自定义帖子类型是WordPress的强大功能,可用于分组不适合帖子和页面类型的数据或发布条目。蛋糕上的锦上添花是通过注册自定义分类法进一步对自定义帖子类型的帖子进行分类的能力。
>关于WordPress自定义帖子类型,通知和分类法的常见问题
>
>如何在WordPress中创建自定义帖子类型?>在WordPress中创建自定义帖子类型涉及在functions.php文件中添加几行代码。您需要使用register_post_type()函数,该函数允许您通过其标签,支持的功能,可用性和其他条件来定义新的帖子类型。请记住,通过访问永久链接设置页面添加代码后,要刷新您的重写规则。> WordPress中的WordPress分类法是什么?自定义帖子类型)在一起。它们有两种形式:类别和标签。类别是分层的,可以具有子类别,而标签不是层次结构。您可以使用register_taxonomy()函数创建自定义分类法。>
>在注册时如何将自定义分类法与自定义邮政类型相关联?带有register_taxonomy()函数的自定义分类法,您可以指定该分类法应该与之关联的帖子类型。这是通过将帖子类型名称作为第二个参数的数组来完成的。自定义更新帖子时显示的消息。这对于自定义帖子类型特别有用,您可能希望在其中显示与默认的消息不同的消息。
>如何将自定义字段添加到WordPress中的自定义帖子类型?使用“ add_meta_box”函数在WordPress中进行自定义帖子类型。此功能允许您在帖子编辑屏幕中添加一个新的Meta框,您可以在其中输入帖子的其他信息。
>在使用register_taxonomy()函数注册自定义分类法时,您可以指定该分类法应与之关联的邮政类型。这是通过将帖子类型名称作为第二个参数的数组来完成的。每当创建或更新帖子或页面时,在WordPress中都会触发。它可用于执行诸如保存元数据,发送通知或保存后应发生的其他任务之类的操作。
>以上是WordPress自定义帖子类型 - 通知和分类法的详细内容。更多信息请关注PHP中文网其他相关文章!