Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 无法创建父项>;儿童>;自定义帖子类型上的孙子关系_Wordpress_Custom Post Type_Relationships - Fatal编程技术网

Wordpress 无法创建父项>;儿童>;自定义帖子类型上的孙子关系

Wordpress 无法创建父项>;儿童>;自定义帖子类型上的孙子关系,wordpress,custom-post-type,relationships,Wordpress,Custom Post Type,Relationships,我有三种自定义帖子类型: –承诺(家长、顶级) -文章(儿童,属于儿童) -章节(孙辈,属于文章,而孙辈则属于承诺) 我可以创建三种自定义帖子类型。可以创建承诺,可以创建文章并为其分配父承诺,可以创建节,但我无法让文章显示在自定义元框中,以将其分配为父承诺 目前,它都封装在一个插件中,如下所示 任何指向正确方向的指点都将非常感激——我感到困惑 //Register Cutom Post Types function custom_post_types(){ // C R E A T

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

承诺(家长、顶级)

-文章(儿童,属于儿童)

-章节(孙辈,属于文章,而孙辈则属于承诺)

我可以创建三种自定义帖子类型。可以创建承诺,可以创建文章并为其分配父承诺,可以创建节,但我无法让文章显示在自定义元框中,以将其分配为父承诺

目前,它都封装在一个插件中,如下所示

任何指向正确方向的指点都将非常感激——我感到困惑

//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;
            } 
        }
};

创建成为父母的承诺


我在每一步都向您的代码中添加调试,并看到wp_下拉页面返回为空。意识到我没有承诺。创建了一个誓言。出现下拉列表。

通过将
'hierarchical'=>false
添加到传递到
wp\u下拉页面的参数列表中,此问题已得到解决


如前所述,在创建下拉列表时,
get_pages()
的一些参数可以起作用,但是当您想要实现相反的效果时,将hierarchy设置为false似乎有点违反直觉。

这很有趣。您可能会认为,使用show\u option\u none和option\u none\u值,您至少会得到一个带有固定选项的下拉列表。但当没有可能的家长时,这并没有起到作用。如果我创建了一篇没有链接到家长承诺的文章,该文章会显示在“部分”仪表板上的下拉列表中,这提示
wp\u dropdown\u pages()
仅查询孤立页面。情节越来越复杂了@anmari!嗯,我试着抛出'depth'和显式'parent'=-1,但没有区别。它调用get_pages(获取页面)-代码中没有任何明显的内容跳到我身上。我必须承认,我想知道在不同类型的帖子中使用parents(家长)是否会有问题-有些问题得到了解决,因为wp没有预料到这一点。但这并不特别有意义。甚至考虑到缓存-为什么只有孤儿?所以我最终得到了
$pages=wp\u dropdown\u pages(数组('post\u type'=>$parent,'selected'=>$post->post\u parent,'name'=>'parent\u id','show\u option\u none'=>\uu('no parent')),'option\u none\u value'=>'none','sort\u column'=>'菜单顺序,post\u title','echo'=>0,'depth'=>-1,'parent'=>-1,'hierarchical'=>false