以编程方式添加自定义帖子

时间:2011-07-18 作者:Matt G

我在“init”操作中添加了一个自定义帖子类型,如下所示:

// Setup the \'Classifieds\' Post Type
    $labels = array(
        \'name\' =>                   _x(\'Classifieds\', \'post type general name\'),
        \'singular_name\' =>          _x(\'Classifieds\', \'post type singular name\'),
        \'add_new\' =>                _x(\'Add New\', \'classifieds\'),
        \'add_new_item\' =>           __(\'Add Classifieds Item\'),
        \'edit_item\' =>              __(\'Edit Classifieds\'),
        \'new_item\' =>               __(\'New Classifieds Item\'),
        \'view_item\' =>              __(\'View Classifieds\'),
        \'search_items\' =>           __(\'Search Classifieds\'),
        \'not_found\' =>              __(\'No classifieds found\'),
        \'not_found_in_trash\' =>     __(\'No classifieds found in Trash\'), 
        \'parent_item_colon\' =>      \'\',
        \'menu_name\' =>              \'Classifieds\'
    );

    // Configure the \'Classifieds\' post type
    $args = array(
        \'labels\' =>                 $labels,
        \'public\' =>                 true,
        \'publicly_queryable\' =>     true,
        \'show_ui\' =>                true,
        \'show_in_menu\' =>           true, 
        \'query_var\' =>              true,
        \'rewrite\' =>                array("slug" => "classifieds"),
        \'capability_type\' =>        \'post\',
        \'has_archive\' =>            true, 
        \'hierarchical\' =>           true,
        \'menu_position\' =>          5,                                              
        \'supports\' =>               array(\'title\',
                                        \'editor\',
                                        \'author\',
                                        \'thumbnail\',
                                        \'excerpt\',
                                        \'trackbacks\',
                                        \'custom-fields\',
                                        \'revisions\',
                                        \'comments\',
                                        add_theme_support( \'post-formats\', 
                                                            array(\'aside\', \'gallery\')
                                                        )
                                    ),
        \'taxonomies\' => array(\'category\', \'post_tag\')
    );
    // Add the \'Classifieds\' post type
    register_post_type(\'classifieds\', $args);
我的问题是,当我想以编程方式插入自定义帖子时,如下所示:

$cat_ID = get_cat_ID( \'calendar\' );

            $data = array(
                \'post_content\' => stripslashes($output),
                \'post_title\' => stripslashes($title),
                \'post_date\' => date(\'Y-m-d H:i:s\'),
                \'post_category\' => array($cat_ID),
                \'post_status\' => $statusPost,
                \'post_author\' => $post_author
            );
            $post_id = wp_insert_post($data);
自定义帖子显示在管理员的通用“帖子”类型帖子下,而不是我在上面创建的自定义帖子类型“分类广告”下。

有人有什么建议吗?

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

您需要在$data 阵列:

$data = array(
    \'post_type\' => \'classifieds\',
    \'post_content\' => stripslashes($output),
    \'post_title\' => stripslashes($title),
    \'post_date\' => date(\'Y-m-d H:i:s\'),
    \'post_category\' => array($cat_ID),
    \'post_status\' => $statusPost,
    \'post_author\' => $post_author
);

结束

相关推荐