如何在url中没有ategory_base的情况下获取特定类别的档案?

时间:2013-02-22 作者:Mike

我使用此代码获取特定类别的存档:

function extend_date_archives_add_rewrite_rules($wp_rewrite){
    $rules = array();
    $structures = array(
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
    );
    foreach( $structures as $s ){
        $rules += $wp_rewrite->generate_rewrite_rules($s);
    }
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action(\'generate_rewrite_rules\', \'extend_date_archives_add_rewrite_rules\');
它工作得很好,我得到了一个URL,如:example。com/category\\u base/category\\u name/date/YYYY/MM

问题是我想要一个没有category\\u base的URL:example。com/category\\u name/date/YYYY/MM

如果我使用插件删除de“category\\u base”(如WP no category base) 或者简单地使用“str\\u replace”删除“category\\u base”,我在所有页面和单个页面中都会得到一个404错误页面。

我能做什么?

谢谢

Added notes:

如果我使用:

function extend_date_archives_flush_rewrite_rules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
add_action(\'init\', \'extend_date_archives_flush_rewrite_rules\');
function extend_date_archives_add_rewrite_rules($wp_rewrite){
    $rules = array();
    $structures = array(
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
        $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
    );
    foreach( $structures as $s ){
        $rules += $wp_rewrite->generate_rewrite_rules($s);
    }
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action(\'generate_rewrite_rules\', \'extend_date_archives_add_rewrite_rules\');
示例。com/category\\u name/YYYY工作正常,但single和pages转到404页。但如果我只使用:

function extend_date_archives_add_rewrite_rules($wp_rewrite){
        $rules = array();
        $structures = array(
            $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
            $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
            $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
        );
        foreach( $structures as $s ){
            $rules += $wp_rewrite->generate_rewrite_rules($s);
        }
        $wp_rewrite->rules = $rules + $wp_rewrite->rules;
    }
    add_action(\'generate_rewrite_rules\', \'extend_date_archives_add_rewrite_rules\');
然后,所有的单页和页都可以正常工作,但示例除外。com/category\\U name/YYYY转到404。有什么想法吗?我刷新permalinks,然后继续处理问题:(

1 个回复
SO网友:Diana

Update: 已尝试并正常工作only without 这个WP no category base 插件,这是,你必须处理类别,虽然你可以在永久链接设置中自由更改它。

对于单个帖子中的完整URL,请在中设置类别结构Settings > Permalinks, 然后在类别库中输入/%category%/%postname%/ (下图)。

Custom Structure must be <code>/%category%/%postname%/</code>

测试的功能是:

function extend_date_archives_flush_rewrite_rules(){
   global $wp_rewrite;
   $wp_rewrite->flush_rules();
}
add_action(\'init\', \'extend_date_archives_flush_rewrite_rules\');

function extend_date_archives_add_rewrite_rules($wp_rewrite){
$rules = array();
$structures = array(
    $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_date_permastruct(),
    $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_month_permastruct(),
    $wp_rewrite->get_category_permastruct() . $wp_rewrite->get_year_permastruct(),
);
foreach( $structures as $s ){
    $rules += $wp_rewrite->generate_rewrite_rules($s);
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}

add_action(\'generate_rewrite_rules\',\'extend_date_archives_add_rewrite_rules\');
然后您可以访问年份category/categoryname/2012/02, 月category/categoryname/2012/02 和白天site.com/category/the-categoryname/2012/02/25.

我认为您可以尝试将插件重写与此函数合并,因为两次重写似乎会导致一些冲突。

结束