自定义分类导致404页-永久链接结构刷新

时间:2013-10-17 作者:Howdy_McGee

我有一个CPT:

function CPT_init(){
    register_post_type(\'custcpt\', array(
        \'labels\'            =>  array(
            \'name\'          =>      __(\'Products\'),
            \'singular_name\' =>      __(\'Product\'),
            \'all_items\'     =>      __(\'View Products\'),
            \'add_new\'       =>      __(\'New Product\'),
            \'add_new_item\'  =>      __(\'New Product\'),
            \'edit_item\'     =>      __(\'Edit Product\'),
            \'view_item\'     =>      __(\'View Product\'),
            \'search_items\'  =>      __(\'Search Products\'),
            \'no_found\'      =>      __(\'No Products Found\'),
            \'not_found_in_trash\' => __(\'No Products in Trash\')
                                ),
        \'public\'            =>  true,
        \'publicly_queryable\'=>  true,
        \'show_ui\'           =>  true, 
        \'query_var\'         =>  true,
        \'show_in_nav_menus\' =>  false,
        \'capability_type\'   =>  \'post\',
        \'hierarchical\'      =>  false,
        \'rewrite\'           =>  array(\'slug\' => \'/products/\', \'with_front\' => false),
        \'menu_position\'     =>  21,
        \'supports\'          =>  array(\'title\',\'editor\', \'thumbnail\'),
        \'has_archive\'       =>  true
    ));
}
add_action(\'init\', \'CPT_init\');
以及我的CPT分类:

function custcpt_category_taxonomy() {
    $labels = array(
        \'name\'              => __( \'Product Categories\' ),
        \'singular_name\'     => __( \'Product Category\' ),
        \'search_items\'      => __( \'Search Product Categories\' ),
        \'all_items\'         => __( \'All Product Categories\' ),
        \'parent_item\'       => __( \'Parent Product Category\' ),
        \'parent_item_colon\' => __( \'Parent Product Category:\' ),
        \'edit_item\'         => __( \'Edit Product Category\' ), 
        \'update_item\'       => __( \'Update Product Category\' ),
        \'add_new_item\'      => __( \'Add New Product Category\' ),
        \'new_item_name\'     => __( \'New Product Category\' ),
        \'menu_name\'         => __( \'Product Categories\' ),
    ); 
    $args = array(
        \'labels\'            => $labels,
        \'public\'            =>  true,
        \'hierarchical\'      =>  true,
        \'show_in_nav_menus\' =>  true,
        \'has_archive\'       =>  true,
        \'rewrite\'           =>  array(\'slug\' => \'/products/\', \'with_front\' => false),
    );
    register_taxonomy( \'custtax\', \'custcpt\', $args );
}
add_action( \'init\', \'custcpt_category_taxonomy\');
我有一个产品页面/products/ 和分类模板页:taxonomy-custtax.php, 我试过了taxonomy.php 但这似乎并不重要。到目前为止,我已经冲洗了15-20次permalinks。它可以在默认情况下正常工作,但在%postname%. 我在分类法上方添加了以下内容:

function custom_taxonomy_flush_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
add_action(\'init\', \'custom_taxonomy_flush_rewrite\');
所以当我去http://www.mywebsite.com/products/term 我因为某种原因得到了404。我发誓分类法总是给我带来问题,每一个。仅有一个的时间

2 个回复
最合适的回答,由SO网友:random_user_name 整理而成

删除重写段上的斜杠。应该是这样的:

 ...
 \'rewrite\'           =>  array(\'slug\' => \'products\', \'with_front\' => false)
 ...
此外,分类法和自定义帖子类型不能使用相同的slug。它们必须是唯一的,否则WP将不知道哪个应用于哪个。

此外,如果您的自定义帖子类型的slug为products, 您不能有一个包含少量products 此外,否则会导致冲突。

最后,对于这些文件的自定义分类列表,您希望文件的标题为taxonomy-{taxonomy_name}.php, 所以在你的情况下,它应该被命名为taxonomy-custtax.php.

为了好运,我建议分步骤进行:

首先,设置自定义帖子类型。然后,如果你将slug设置为products (刷新重写规则后),请访问www.mysite。com/产品(再次确认nothing 您安装的其他软件中有大量产品)。您应该看到您的产品列表(假设您添加了一些产品条目)。

然后,添加archive-custcpt.php 文件,以某种独特的方式对其进行修改,然后刷新。您应该看到,它现在正在该新模板中加载您的产品列表。

然后,添加自定义分类法。如果你的分类学鼻涕虫是prodcat (对于产品类别),您应该可以访问www.mysite。com/prodcat/[术语](其中[术语]=您已设置的产品类别)。

最后,添加您的taxonomy-custtax.php 文件,以某种独特的方式修改它,然后刷新。您应该会在新模板文件中看到该列表。

SO网友:Vivekanand Saraswati

从两个slug中删除“/”并创建一个文件taxonomy-custtax.php 那就好了。

我在下面引用您的代码并更正:

function CPT_init(){
    register_post_type(\'custcpt\', array(
        \'labels\'            =>  array(
            \'name\'          =>      __(\'Products\'),
            \'singular_name\' =>      __(\'Product\'),
            \'all_items\'     =>      __(\'View Products\'),
            \'add_new\'       =>      __(\'New Product\'),
            \'add_new_item\'  =>      __(\'New Product\'),
            \'edit_item\'     =>      __(\'Edit Product\'),
            \'view_item\'     =>      __(\'View Product\'),
            \'search_items\'  =>      __(\'Search Products\'),
            \'no_found\'      =>      __(\'No Products Found\'),
            \'not_found_in_trash\' => __(\'No Products in Trash\')
                                ),
        \'public\'            =>  true,
        \'publicly_queryable\'=>  true,
        \'show_ui\'           =>  true, 
        \'query_var\'         =>  true,
        \'show_in_nav_menus\' =>  false,
        \'capability_type\'   =>  \'post\',
        \'hierarchical\'      =>  false,
        \'rewrite\'           =>  array(\'slug\' => \'/products\', \'with_front\' => false),
        \'menu_position\'     =>  21,
        \'supports\'          =>  array(\'title\',\'editor\', \'thumbnail\'),
        \'has_archive\'       =>  true
    ));
}
add_action(\'init\', \'CPT_init\');


function custcpt_category_taxonomy() {
    $labels = array(
        \'name\'              => __( \'Product Categories\' ),
        \'singular_name\'     => __( \'Product Category\' ),
        \'search_items\'      => __( \'Search Product Categories\' ),
        \'all_items\'         => __( \'All Product Categories\' ),
        \'parent_item\'       => __( \'Parent Product Category\' ),
        \'parent_item_colon\' => __( \'Parent Product Category:\' ),
        \'edit_item\'         => __( \'Edit Product Category\' ), 
        \'update_item\'       => __( \'Update Product Category\' ),
        \'add_new_item\'      => __( \'Add New Product Category\' ),
        \'new_item_name\'     => __( \'New Product Category\' ),
        \'menu_name\'         => __( \'Product Categories\' ),
    ); 
    $args = array(
        \'labels\'            => $labels,
        \'public\'            =>  true,
        \'hierarchical\'      =>  true,
        \'show_in_nav_menus\' =>  true,
        \'has_archive\'       =>  true,
        \'rewrite\'           =>  array(\'slug\' => \'/products\', \'with_front\' => false),
    );
    register_taxonomy( \'custtax\', \'custcpt\', $args );
}
add_action( \'init\', \'custcpt_category_taxonomy\');

结束

相关推荐