无法在自定义帖子类型上创建父>子>孙子关系

时间:2018-08-08 20:15:20

标签: wordpress custom-post-type relationships

我有三种自定义帖子类型:

承诺 (父级,顶层)

文章 (孩子,属于承诺)

部分 (孙子,属于“条款”,又属于“承诺”)

我可以创建三种自定义帖子类型。可以创建认捐,可以创建文章并为其指定父项誓词,可以创建栏目,但是我无法在自定义元框中显示要显示的文章来为其指定父项。

此刻在下面,它们全部包装在一个插件中。

任何朝着正确方向的指针将不胜感激–我感到困惑。

//Register Cutom Post Types

function custom_post_types(){


    // C R E A T E   P L E D G E S

    $labels = array(
        'name' => 'Pledges',
        'singular_name' => 'Pledge',
        'menu_name' => 'Pledges',
        'admin_menu_bar' => 'Pledges',
        'add_new' => 'Add New Pledge',
        'add_new_item' => 'Add New Pledge',
        'view_item' => 'View Pledge',
        'view_items' => 'View Pledges'
    );

    $args = array(
        'label' => 'Pledges',
        'description' => 'Anti Corruption Pledges',
        'labels' => $labels,
        'menu_icon' => 'dashicons-awards',
        'supports' => array(),
        'taxonomies' => array('pledge'),
        'hierarchical' => true,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'query_var' => 'pledges',
        'rewrite' => array('slug' => 'pledges')
    );

    register_post_type('pledge', $args);


    // C R E A T E   A R T I C L E S

    $labels = array(
        'name' => 'Articles',
        'singular_name' => 'Article',
        'menu_name' => 'Articles',
        'admin_menu_bar' => 'Articles',
        'parent_item_colon' => 'Pledge:',
        'add_new' => 'Add New Article',
        'add_new_item' => 'Add New Article',
        'view_item' => 'View Article',
        'view_items' => 'View Articles'
    );

    $args = array(
        'label' => 'Articles',
        'description' => 'A Pledge\'s article',
        'labels' => $labels,
        'menu_icon' => 'dashicons-media-text',
        'supports' => array(),
        'taxonomies' => array('category'),
        'hierarchical' => true,
        'public' => true,
        'show_ui' => true,
        'show_in_menu'=> true,
        'menu_position' => 6,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'query_var' => 'articles',
        'rewrite' => array('slug' => 'articles')
    );

    register_post_type('article', $args);


    // C R E A T E   S E C T I O N S

    $labels = array(
        'name' => 'Sections',
        'singular_name' => 'Section',
        'menu_name' => 'Sections',
        'admin_menu_bar' => 'Sections',
        'parent_item_colon' => 'Article:',
        'add_new' => 'Add New Section',
        'add_new_item' => 'Add New Section',
        'view_item' => 'View Section',
        'view_items' => 'View Sections'
    );

    $args = array(
        'label' => 'Sections',
        'description' => 'An Articles\' section',
        'labels' => $labels,
        'menu_icon' => 'dashicons-format-aside',
        'supports' => array(),
        'hierarchical' => true,
        'public' => true,
        'show_ui' => true,
        'show_in_menu'=> true,
        'menu_position' => 7,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'query_var' => 'sections',
        'rewrite' => array('slug' => 'sections')
    );

    register_post_type('section', $args);
}

add_action('init', 'custom_post_types', 0);



//Create meta boxes for relationships between Pledges and Articles and Sections

//Step One
add_action('admin_menu', function(){
    remove_meta_box('pageparentdiv', array('article', 'section'), 'normal');    
});

//Step Two
add_action('add_meta_boxes', function(){
    add_meta_box('pledge_article-parent', 'Parent content', 'pledge_article_attributes_meta_box', array('article', 'section'), 'side', 'high');
});

//Step Three
function pledge_article_attributes_meta_box($post) {    
    $post_type_object = get_post_type_object($post->post_type);

    if ($post->post_type == 'article') {
        $parent = 'pledge';
    } else {
        $parent = 'article';        
    }
    echo $parent;

    if ( $post_type_object->hierarchical ) {
            $pages = wp_dropdown_pages(array(
                    'post_type'     => $parent, 
                    'selected'      => $post->post_parent, 
                    'name'          => 'parent_id', 
                    'show_option_none'  => __('(no parent)'), 
                    'sort_column'       => 'menu_order, post_title', 
                    'echo'          => 0
        ));

            if ( ! empty($pages) ) {
                echo $pages;
            } 
        }
};

2 个答案:

答案 0 :(得分:0)

创建一个作为父母的誓言。

我在每一步都对您的代码进行调试,发现wp_dropdown_pages变空了。意识到我没有任何承诺。创建了一个承诺。出现下拉菜单。

答案 1 :(得分:0)

已通过在传递给'hierarchical' => false的参数列表中添加wp_dropdown_pages来解决此问题。

正如documentation所述,get_pages()的某些参数在创建下拉列表时可以使用,但是如果您想实现相反的设置,则将层级设置为false似乎有点违反直觉。