REGISTER_POST_TYPE如何知道如何使用ADD_ACTION函数中的哪个函数?

时间:2014-08-15 作者:johnny

This 来自WordPress网站,我在文档中找不到我的答案。当我发布register_post_type, 它如何知道要使用哪个函数?我知道init 已使用自定义挂钩填充操作codex_book_init, 但当我看register\\u post\\u类型时。它只有“书”:

add_action( \'init\', \'codex_book_init\' );

register_post_type( \'book\', $args );
这是如何工作的?不询问所有$args, 只有与此问题相关的一个(s)。

add_action( \'init\', \'codex_book_init\' );
/**
 * Register a book post type.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_post_type
 */
function codex_book_init() {
    $labels = array(
        \'name\'               => _x( \'Books\', \'post type general name\', \'your-plugin-textdomain\' ),
        \'singular_name\'      => _x( \'Book\', \'post type singular name\', \'your-plugin-textdomain\' ),
        \'menu_name\'          => _x( \'Books\', \'admin menu\', \'your-plugin-textdomain\' ),
        \'name_admin_bar\'     => _x( \'Book\', \'add new on admin bar\', \'your-plugin-textdomain\' ),
        \'add_new\'            => _x( \'Add New\', \'book\', \'your-plugin-textdomain\' ),
        \'add_new_item\'       => __( \'Add New Book\', \'your-plugin-textdomain\' ),
        \'new_item\'           => __( \'New Book\', \'your-plugin-textdomain\' ),
        \'edit_item\'          => __( \'Edit Book\', \'your-plugin-textdomain\' ),
        \'view_item\'          => __( \'View Book\', \'your-plugin-textdomain\' ),
        \'all_items\'          => __( \'All Books\', \'your-plugin-textdomain\' ),
        \'search_items\'       => __( \'Search Books\', \'your-plugin-textdomain\' ),
        \'parent_item_colon\'  => __( \'Parent Books:\', \'your-plugin-textdomain\' ),
        \'not_found\'          => __( \'No books found.\', \'your-plugin-textdomain\' ),
        \'not_found_in_trash\' => __( \'No books found in Trash.\', \'your-plugin-textdomain\' )
    );

    $args = array(
        \'labels\'             => $labels,
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => array( \'slug\' => \'book\' ),
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
    );

    register_post_type( \'book\', $args );
}

2 个回复
最合适的回答,由SO网友:sakibmoon 整理而成

您混淆了函数和挂钩。register_post_type() 是一个函数,而不是挂钩。

当您使用register_post_type( \'book\', $args ), 您正在呼叫register_post_type() 函数创建名为book.

register_post_type() 需要在上使用init 钩子是在wordpress初始化开始时启动的,这就是为什么您要使用add_action( \'init\', \'codex_book_init\' ) 和使用register_post_type() 那里

SO网友:Joey Yax

book是您正在注册的帖子类型的名称。然后它引用$args 数组定义自定义帖子类型的属性。它可以这么简单地写:

add_action( \'init\', \'codex_book_init\' );

function codex_book_init() {
  register_post_type(
    \'book\',
     array( // Arguments
       \'labels\'             => array(
         \'name\'               => _x( \'Books\', \'post type general name\', \'your-plugin-textdomain\' ),
         \'singular_name\'      => _x( \'Book\', \'post type singular name\', \'your-plugin-textdomain\' ),
         \'menu_name\'          => _x( \'Books\', \'admin menu\', \'your-plugin-textdomain\' ),
         \'name_admin_bar\'     => _x( \'Book\', \'add new on admin bar\', \'your-plugin-textdomain\' ),
         \'add_new\'            => _x( \'Add New\', \'book\', \'your-plugin-textdomain\' ),
         \'add_new_item\'       => __( \'Add New Book\', \'your-plugin-textdomain\' ),
         \'new_item\'           => __( \'New Book\', \'your-plugin-textdomain\' ),
         \'edit_item\'          => __( \'Edit Book\', \'your-plugin-textdomain\' ),
         \'view_item\'          => __( \'View Book\', \'your-plugin-textdomain\' ),
         \'all_items\'          => __( \'All Books\', \'your-plugin-textdomain\' ),
         \'search_items\'       => __( \'Search Books\', \'your-plugin-textdomain\' ),
         \'parent_item_colon\'  => __( \'Parent Books:\', \'your-plugin-textdomain\' ),
         \'not_found\'          => __( \'No books found.\', \'your-plugin-textdomain\' ),
         \'not_found_in_trash\' => __( \'No books found in Trash.\', \'your-plugin-textdomain\' )
       ),
       \'public\'             => true,
       \'publicly_queryable\' => true,
       \'show_ui\'            => true,
       \'show_in_menu\'       => true,
       \'query_var\'          => true,
       \'rewrite\'            => array( \'slug\' => \'book\' ),
       \'capability_type\'    => \'post\',
       \'has_archive\'        => true,
       \'hierarchical\'       => false,
       \'menu_position\'      => null,
       \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
     )
   );
  }

结束

相关推荐