【发布时间】:2019-05-03 00:04:13
【问题描述】:
我需要一个帖子类型作为页面的子(实际上基本上是任何其他帖子类型及其本身),并且它也必须适用于 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,
)
);
【问题讨论】:
-
我假设
$cpt_name是在代码中的某处定义的,但你有一个错字(在问题中) -$cpy我认为应该是$cpt。cpt_parent_page的示例值是什么? -
@SallyCJ 感谢您抽出宝贵时间,并注意到我已更正的错字。 `` 的示例值为
255。但是,如您所见,我使用get_permalink()来获取我的 URL 重写的格式。 -
抱歉,我没有真正注意到代码中的
get_permalink()。但是您不应该使用它..请检查我的答案并告诉我。 (如果您不想要/p或/t..,我们可以尝试一下。)
标签: php wordpress custom-post-type