固定链接重写404冲突-WordPress分类/CPT

时间:2011-05-10 作者:Joe

想知道是否有人能告诉我为什么子页面(子页面)不再有效(例如:www.mysite.com/type/childpage)。

我有一个名为“Type”的页面(工作正常),我希望有“Type”的子页面(404个错误)-正如您所看到的,我还希望在我的自定义帖子类型/分类之前重写“Type”slug。

我尝试了此链接中的方法,但没有成功:https://wordpress.stackexchange.com/questions/4127/custom-taxonomy-and-pages-rewrite-slug-conflict-gives-404

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

function create_zombie_type() {
register_post_type(\'zombies\', 
array(  
\'description\' => \'Zombie custom post type\',
\'show_ui\' => true,
\'menu_position\' => 5,
\'menu_icon\' => get_stylesheet_directory_uri() . \'/images/zombies.png\',
\'exclude_from_search\' => false,
\'labels\' => array(
    \'name\' => \'Zombies\',
    \'singular_name\' => \'Zombie\',
    \'add_new\' => \'Add New Zombie\',
    \'add_new_item\' => \'Add New Zombie\',
    \'edit\' => \'Edit Zombies\',
    \'edit_item\' => \'Edit Zombie\',
    \'new_item\' => \'New Zombie\',
    \'view\' => \'View Zombie\',
    \'view_item\' => \'View Zombie\',
    \'search_items\' => \'Search Zombies\',
    \'not_found\' => \'No zombies found\',
    \'not_found_in_trash\' => \'No zombies found in Trash\',
    \'parent\' => \'Parent Zombie\',
),
\'hierarchical\' => false,
\'supports\' => array(\'title\',\'editor\',\'excerpt\', \'trackbacks\',\'custom-fields\',     \'comments\',\'revisions\',\'thumbnail\',\'author\',\'page-attributes\'),
\'public\' => true,
\'rewrite\' => array(\'slug\' => \'type/%freshness%\', \'with_front\' => false),
\'taxonomies\' => array(\'freshness\')
) 
);
}




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

function create_zombie_taxonomies() {
register_taxonomy( 
\'freshness\', 
\'zombies\',
array( 
\'labels\' => array(
    \'name\' => \'Freshness\',
    \'singular_name\' => \'Freshness\',
    \'search_items\' =>  \'Search Freshness\',
    \'popular_items\' => \'Popular Freshness\',
    \'all_items\' => \'All Freshness\',
    \'parent_item\' => \'Parent Freshness\',
    \'parent_item_colon\' => \'Parent Freshness:\',
    \'edit_item\' => \'Edit Freshness\', 
    \'update_item\' => \'Update Freshness\',
    \'add_new_item\' => \'Add New Freshness\',
    \'new_item_name\' => \'New Freshness Name\'
), 
\'hierarchical\' => true, 
\'public\' => true,
\'show_ui\' => true,
\'query_var\' => \'freshness\',
\'show_tagcloud\' => true,
\'rewrite\' => array( \'slug\' => \'type\', \'with_front\' => false ) 
) 
   );

}



function filter_post_type_link($link, $post)
{
if ($post->post_type != \'zombies\')
return $link;

if ($cats = get_the_terms($post->ID, \'freshness\'))
$link = str_replace(\'%freshness%\', array_pop($cats)->slug, $link);
return $link;
}

add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);

1 个回复
SO网友:Jan Fabry

可以为以开头的每个页面创建详细的页面规则/type/, 但您还需要连接到页面创建和类似的内容,以保持最新。如果你没有那么多额外的页面,那么对所有页面强制使用详细的页面规则可能会更容易,然后WordPress会为你这样做。

add_action( \'init\', \'wpse16902_init\' );
function wpse16902_init() {
    $GLOBALS[\'wp_rewrite\']->use_verbose_page_rules = true;
}
现在唯一的问题是,分类规则甚至被放在了上面(我认为是从WP 3.1开始)。但有了正确的钩子,我们就可以截取页面规则,并将它们真正置于顶部。

add_filter( \'page_rewrite_rules\', \'wpse16902_collect_page_rewrite_rules\' );
function wpse16902_collect_page_rewrite_rules( $page_rewrite_rules )
{
    $GLOBALS[\'wpse16902_page_rewrite_rules\'] = $page_rewrite_rules;
    return array();
}

add_filter( \'rewrite_rules_array\', \'wspe16902_prepend_page_rewrite_rules\' );
function wspe16902_prepend_page_rewrite_rules( $rewrite_rules )
{
    return $GLOBALS[\'wpse16902_page_rewrite_rules\'] + $rewrite_rules;
}
对于/type/%freshness%/ URL。您希望这些是分类法存档,而不是自定义的帖子类型存档。如果对此有问题,请颠倒注册顺序:首先是分类法,然后是自定义帖子类型:

register_taxonomy(
    \'freshness\',
     array(), // Empty array, no post types linked
     array(
         \'rewrite\' => array(
             \'slug\' => \'type\',
         )
     ),
 );
 register_post_type(
     \'zombie\',
     array(
         \'rewrite\' => array(
             \'slug\' => \'type/%freshness%\',
         ),
         \'taxonomies\' => array( \'freshness\' ),
     ),
 );
额外注意:如果你的分类URL真的以/type/, 您可能会与后格式归档发生冲突。/type/aside/ 默认情况下,显示所有“旁白”格式的帖子。这个答案假设僵尸自定义帖子类型和分类法只是一个示例,您在实际问题中使用了另一种URL格式

结束

相关推荐

curl problem or permalinks

我刚刚配置了我的VPS,我使用的是Centos,一切都很好,但如果我将永久链接设置为自定义结构,然后接受主页,没有帖子出现,它会显示404页,我想这是因为我没有启用curl,但我不知道我的php在哪里。我的centos中的ini文件?好的,我的卷曲被启用了,我检查过了phpinfo(); 这里是URLhttp://74.117.158.182/info.php但如果我在我的wordpress中设置了永久链接,那么接受主页,所有都会给我404页,你可以在这个URL上查看http://mbas.co.in如果