自定义帖子类型和复杂的内容层次结构

时间:2013-07-06 作者:intheusa

我正在开发一个相当复杂的公文包WP主题,但我似乎无法找到为内容设置层次结构的逻辑和最有效的方法。内容需要很深的层次结构,我需要面包屑和菜单才能工作。自定义帖子类型(CPT)用于“客户端工作”,理想情况下,它是默认页面结构中工作>>客户端>>客户端(CPT)的子页面。但我无法为CPT定义父页面。我正在相应地重写slug,但这似乎无法维护父/子关系。

目前,我有如下结构:

Work > Clients >> Client >>> Client Work(CPT) >>> Single Work Item

我的问题是:1。有没有更好的方法来组织这个?2、我是否应该不使用CPT,或者我如何将CPT添加到更多的层次结构中,以便它们与各自的父母相关?

这是我的CPT注册码:

class cpt_client_work {

function cpt_client_work() {
    add_action(\'init\',array($this,\'create_post_type\'));
}

function create_post_type() {
    $labels = array(
    \'name\' => \'Client Work\',
    \'singular_name\' => \'Client\',
    \'add_new\' => \'New Client Work\',
    \'all_items\' => \'All Client Work\',
    \'add_new_item\' => \'New Client Work\',
    \'edit_item\' => \'Edit Client Work\',
    \'new_item\' => \'New Client Work\',
    \'view_item\' => \'View Client Work\',
    \'search_items\' => \'Search Client Work\',
    \'not_found\' =>  \'No Client Work found\',
    \'not_found_in_trash\' => \'No Client Work found in trash\',
    \'parent_item_colon\' => \'Parent Client:\',
    \'parent\' => \'Client\',
    \'menu_name\' => \'Client Work\'
    );
    $args = array(
        \'labels\' => $labels,
        \'description\' => "Client Work",
        \'public\' => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\' => true,
        \'show_ui\' => true, 
        \'show_in_nav_menus\' => true, 
        \'show_in_menu\' => true,
        \'show_in_admin_bar\' => true,
        \'menu_position\' => 20,
        \'menu_icon\' => get_bloginfo(\'template_directory\') . \'/_/images/admin-icon.png\',
        \'capability_type\' => \'post\',
        \'hierarchical\' => true,
        \'rewrite\' => array(\'slug\' => \'work/client\'),
        \'supports\' => array(\'page-attributes\',\'title\',\'editor\',\'thumbnail\',\'excerpt\',\'custom-fields\',\'revisions\',\'author\'),
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true
    ); 
    register_post_type(\'cpt_client_work\',$args);
}
 }

1 个回复
SO网友:Marco Panichi

你试图把作品和客户放在同一个层次上,这有点令人困惑。我建议您将这两个实体分开,并与上述实体建立所需的所有关系Posts to posts plugin.

首先create a custom type Work 用他自己的子弹():

...
function create_post_type() 
{
    $labels = array( 
        \'name\' => \'Work\',
        ...
    );
    $args = array(
        \'labels\' => $labels,
        \'rewrite\' => array(\'slug\' => \'works\'),
        ...
    ); 
    register_post_type(\'work\',$args);
}
...
那么create the Clients:

...
function create_post_type() 
{
    $labels = array( 
        \'name\' => \'Clients\',
        ...
    );
    $args = array(
        \'labels\' => $labels,
        \'rewrite\' => array(\'slug\' => \'clients\'),
        ...
    ); 
    register_post_type(\'client\',$args);
}
...
现在install the Posts to posts 插入并定义两个实体之间的关系:

function my_connection_types() {
    p2p_register_connection_type( array(
        \'name\' => \'works_to_clients\',
        \'from\' => \'work\',
        \'to\' => \'client\'
    ) );
}
add_action( \'p2p_init\', \'my_connection_types\' );
此时,您的data structure is well built 你可以进入下一个阶段(客户和作品主题、面包屑等)

结束

相关推荐