设为页面的自定义帖子类型

时间:2018-11-30 20:05:40

标签: php wordpress custom-post-type

我需要一个帖子类型作为页面的 child (实际上基本上是任何其他帖子类型及其本身),并且它也必须适用于CPT的分类法。

通常的想法是在我的选项中有一个动态的父页面,保存在cpt_parent_page下。目前,我的弹效果很好;在管理员的CPT分类下,我具有正确的链接。但是,当我查看分类法时,我会得到404。我敢肯定,我只是忘记了一个非常小的事情,但是我需要记住所说的事情。

$cpt = 'book';
$cpt_singular = 'Book';
$cpt_plurial = 'Books';

/*
 * Register CPT
 */
$labels = array(
    'add_new_item' => 'Add New '.$cpt_singular,
    'all_items' => 'All '.$cpt_plurial,
    'edit_item' => 'Edit '.$cpt_singular,
    'name' => $cpt_singular,
    'name_admin_bar' => $cpt_singular,
    'new_item' => 'New '.$cpt_singular,
    'not_found' => 'No '.$cpt_singular.' found',
    'not_found_in_trash' => 'No '.$cpt_plurial.' found in Trash',
    'parent_item_colon' => 'Parent '.$cpt_singular,
    'search_items' => 'Search '.$cpt_plurial,
    'view_item' => 'View '.$cpt_singular,
    'view_items' => 'View '.$cpt_plurial,
);
$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_in_menu' => true,
    'show_in_admin_bar' => true,
    'can_export' => true,
    'has_archive' => false,
    'hierarchical' => true,
    'menu_position' => null,
    'supports' => array( 'editor', 'thumbnail', 'title' ),
    'rewrite' => array(
        'slug' => get_permalink( get_option('cpt_parent_page') ),
        'with_front' => false
    ),
);
register_post_type($cpt, $args);

/*
 * Register taxonomy
 */
register_taxonomy(
    $cpt_name, // Taxonomy name
    $cpt, // Associate taxonomy to this post type
    array(
        'hierarchical' => true,
        'label' => $tax_name,
        'query_var' => true,
        'rewrite' => array(
        'slug' => get_permalink( get_option('cpt_parent_page') ),
            'with_front' => false
        ),
        'show_in_quick_edit' => true,
        'show_admin_column' => true,
    )
);

1 个答案:

答案 0 :(得分:2)

您最有可能收到错误void delByIndex(vector<int>::iterator &i, vector<int>& a) { a.erase(i); } int main() { vector<int> a {1,5,6,2,7,8,3}; vector<int> b {1,2,3,1}; for(auto i=a.begin();i!=a.end();) { bool flag = false; for(auto j=b.begin();j!=b.end();j++) { if(*i==*j) { flag = true; } } if(flag) { delByIndex(i, a); } else i++; } for(auto i:a) cout << i << " "; return 0; } ,这是因为更改选项后尚未刷新重写规则。但是,即使不是这种情况,请确保在更改选项后始终刷新规则 –只需访问永久链接设置页面即可。

除此之外,您不应该使用404,或者您应该使用帖子 slug 而不是其永久链接URL。因此,请进行以下更改:

get_permalink()

那应该起作用;但是,// Change this: get_permalink( get_option('cpt_parent_page') ) // to this FOR THE POST TYPE: get_post_field( 'post_name', get_option('cpt_parent_page') ) . '/p' // or this FOR THE TAXONOMY: get_post_field( 'post_name', get_option('cpt_parent_page') ) . '/t' /p是必需的,以便WordPress知道请求是针对帖子还是术语(按分类法)—如果没有/t/p,重写规则将发生冲突并可能失败(例如,术语请求被混淆为发布或子页面请求)。

相关问题