单个-{Custom}.php 404的或/soronomy/404的-自定义帖子类型和分类固定链接

时间:2012-06-15 作者:Dan C

我一直在玩弄自定义的帖子类型和分类法,现在我已经在很多场合大发雷霆了——我相信我已经阅读了互联网上关于如何解决各种问题的每一个论坛,而且我几乎没有抽雪茄。

我重置了永久链接,添加了刷新重写规则();还有其他的

我可以使用分类法,例如域名。com/products/audio-products,但随后是单个{自定义}。php 404

“products”、“product\\u cat%”或下面代码中使用的一段代码将起作用:

register_taxonomy(\'product_cat\',array(\'product\'), array(  
    \'hierarchical\' => true,  
    \'labels\' => $labels,
\'rewrite\' => array( \'slug\' => \'products/%product_cat%\',\'with_front\' =>FALSE, \'hierarchical\' => true ),
    \'query_var\' => true,  
    \'show_ui\' => true  
 ));  

 add_action( \'init\', \'build_taxonomies\', 0 );  

我可以得到一个{自定义}。php显示,但随后显示域名。com/产品/音频产品将404!!

register_taxonomy(\'product_cat\',array(\'product\'), array(  
    \'hierarchical\' => true,  
    \'labels\' => $labels,
    \'rewrite\' => array( \'slug\' => \'pcat\',\'with_front\' => FALSE, \'hierarchical\' => true ),
    \'query_var\' => true,  
    \'show_ui\' => true  
 ));  

 add_action( \'init\', \'build_taxonomies\', 0 );  
以下是我的完整代码:

$labels = array(  
    \'name\' => _x( \'Product Categories\', \'taxonomy general name\' ),  
    \'singular_name\' => _x( \'Product Category\', \'taxonomy singular name\' ),  
    \'search_items\' =>  __( \'Search Product Categories\' ),  
    \'all_items\' => __( \'All Product Categories\' ),  
    \'parent_item\' => __( \'Parent Category\' ),  
    \'parent_item_colon\' => __( \'Parent Category:\' ),  
    \'edit_item\' => __( \'Edit Product Category\' ),  
    \'update_item\' => __( \'Update Product Category\' ),  
    \'add_new_item\' => __( \'Add Product Category\' ),  
    \'new_item_name\' => __( \'New Product Category\' ),  
    \'menu_name\' => __( \'Product Categories\' )  
  );      

register_taxonomy(\'product_cat\',array(\'product\'), array(  
    \'hierarchical\' => true,  
    \'labels\' => $labels,
    \'rewrite\' => array( \'slug\' => \'products/%product_cat%\',\'with_front\' => FALSE, \'hierarchical\' => true ),
    \'query_var\' => true,  
    \'show_ui\' => true  
 ));  

 add_action( \'init\', \'build_taxonomies\', 0 );  


function build_taxonomies() {  
register_taxonomy( 
\'brands\', 
array( \'product\',\'post\' ), 
array( 
\'hierarchical\' => true, 
\'labels\' => array(
            \'name\' => \'Brands\',
            \'singular_name\' => \'Brand\',
            \'update_item\' => \'Edit Brand\',
            \'add_new_item\' => \'New Brand\',
            \'view_item\' => \'View Brand\',
            \'search_items\' => \'Search Brand\'
        ), 
\'query_var\' => true, 
\'rewrite\' => array( \'slug\' => \'brands\',\'with_front\' => FALSE, \'hierarchical\' => true ), ) );  
}  


add_action(\'init\', \'create_post_type\');

function create_post_type() {
    register_post_type(\'product\', array(
        \'labels\' => array(
            \'name\' => \'Products\',
            \'singular_name\' => \'Product\',
            \'add_new\' => \'Add new product\',
            \'edit_item\' => \'Edit product\',
            \'new_item\' => \'New product\',
            \'view_item\' => \'View product\',
            \'search_items\' => \'Search products\',
            \'not_found\' => \'No products found\',
            \'not_found_in_trash\' => \'No products found in Trash\'
        ),

        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'rewrite\' => array( \'slug\' => \'products\',\'with_front\' => FALSE),
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'custom-fields\',
            \'thumbnail\',
            \'excerpt\'
        ),
        \'menu_position\' => 5,
        \'taxonomies\' => array(\'post_tag\', \'product_cat\' , \'brands\') // this is IMPORTANT
    ));
    flush_rewrite_rules();
}


add_filter( \'pre_get_posts\', \'my_get_posts\' );


function my_get_posts( $query ) {

if ( is_home() && $query->is_main_query() )
    $query->set( \'post_type\', array( \'post\', \'page\', \'product\' ) );

return $query;
}

1 个回复
最合适的回答,由SO网友:Dan C 整理而成

几周过去了,我的答案似乎确实奏效了——听到人们谈论使用过滤器的有趣声音——据我所知,它们是不必要的。

是否/%product_cat% 是否有任何影响我不确定,因此对于我的解决方案,我将其忽略:

Product\\u Cat分类应为:

\'rewrite\' => array( \'slug\' => \'products\',\'with_front\' =>FALSE, \'hierarchical\' => true )
产品自定义帖子类型需要更改为:“重写”=>“数组”(“slug”=>“product”、“with\\u front”=>FALSE),

结束

相关推荐