在插件中创建2个发布类型,一个父项为另一个父项

时间:2016-01-29 作者:Raptor

我创建了一个自定义帖子类型“Book”,并成功地将其作为根目录添加到WordPress的管理菜单中。我可以使用成功列出帖子类型内容/wp-admin/edit.php?post_type=test_book &;可以通过添加/wp-admin/post-new.php?post_type=test_book.

但是,我想添加一个自定义帖子类型“Chapter”(test_chapter) 通过允许用户选择章节(并在post-new.php), 从而建立亲子关系。

我该怎么做?

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

这里需要使用的代码有一些步骤。首先,您需要在自定义帖子类型编辑上创建e元数据库。php,它将列出您所有的test\\u chapter post类型的帖子。然后,您必须挂接save\\u post操作,以便将已编辑帖子的父级设置为所选的“test\\u chapter”。最好的方法是将这两种帖子类型连接为post meta,而不是父子关系,以便更好地处理它。根据本教程:http://wptheming.com/2010/08/custom-metabox-for-post-type/

add_action( \'add_meta_boxes\', \'add_books_metaboxes\' );
    // Add the Book Meta Boxes

    function add_books_metaboxes() {
        add_meta_box(\'wpt_test_chapter\', \'Book chapter\', \'wpt_test_chapter\', \'test_book\', \'side\', \'default\');
    }


// Book chapter Metabox

function wpt_test_chapter() {
    global $post;


    $args = array(
    \'posts_per_page\'   => -1,
    \'post_type\'        => \'test_chapter\',
    );
    $books_array = get_posts( $args );
    $chapter_ids = get_post_meta($post->ID, \'related_chapters\', true);
    ?>
    <select name="book_chapters" multiple>
    <?php
    foreach($books_array as $book_array){
        if(in_array($book_array->ID, $chapter_ids)){$selected = \'selected;\'}
        else{$selected = \'\';}
        echo \'<option \'.$book_array->ID.\' \'.$selected.\'>\'.$book_array->post_title.\'</option>\';
    }
    ?>
    </select>
    <?php
    // Noncename needed to verify where the data originated
    /* if you are using plugin:
    echo \'<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="\' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';
    */
}



// Save the Metabox Data

function wpt_save_chapter_meta($post_id, $post) {

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    /* if you are using plugin:
    if ( !wp_verify_nonce( $_POST[\'eventmeta_noncename\'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }
    */
    // Is the user allowed to edit the post or page?
    if ( !current_user_can( \'edit_post\', $post->ID ))
        return $post->ID;

    // OK, we\'re authenticated: we need to find and save the data
    // We\'ll put it into an array to make it easier to loop though.

    $chapters_ids[\'book_chapters\'] = $_POST[\'book_chapters\'];

    // Add values of $events_meta as custom fields

    foreach ($chapter_ids as $chapter_id) { // Cycle through the $events_meta array!
        //if( $post->post_type == \'revision\' ) return; // Don\'t store custom data twice
        update_post_meta($post->ID, \'related_chapters\', $chapter_id);
    }

}

add_action(\'save_post\', \'wpt_save_chapter_meta\', 1, 2); // save the custom fields
没有测试代码,因此可能会有一些错误。让我知道,这样我可以纠正他们。

SO网友:AjayShanker

首先创建test\\u book函数并调用add_action() 函数初始化此函数。

示例:

add_action(\'admin_menu\', \'test_book\');
然后在定义test\\u book()函数功能后,为了添加菜单和子菜单,您声明function test_book() 像这样。。。。

function test_book(){
    add_menu_page( "Books", "Books", "manage_options", "test_book", "test_book", "" );
    add_submenu_page( "test_book", "Chapters", "Chapters", "manage_options", "test_chapter", "test_chapter");
}

相关推荐