【问题标题】:How to remove the date from the URL of a post in a specific category, without plugin?如何在没有插件的情况下从特定类别的帖子的 URL 中删除日期?
【发布时间】:2021-11-08 10:15:36
【问题描述】:

我需要一些帮助才能找到仅从特定类别的帖子中删除 url 日期的解决方案。

我的永久链接设置如下:

 /%year%/%monthnum%/%day%/%category%/%postname%/

category-x 帖子具有以下 url:

http://mysite/2021/08/25/category-x/post-name/

我只需要 category-x 帖子,网址如下所示:

http://mysite/category-x/post-name/

其他帖子将保留网址中的日期。

我尝试使用 rewrite_rules_array 和 post_link 过滤器做一些事情,但没有成功。

我该如何解决?

【问题讨论】:

标签: php wordpress .htaccess url categories


【解决方案1】:

我是这样解决的:

add_filter( 'post_link', function( $permalink, $post, $leavename ){
    $category = get_the_category($post->ID);    
    if (  !empty($category) && $category[0]->slug == "category-x" ) {
        $permalink = trailingslashit( home_url('/category-x/'. $post->post_name . '/' ) );
    }
    return $permalink;
}, 10, 3 ); 

add_action('generate_rewrite_rules',function( $wp_rewrite ) {
    $new_rules['^category-x/([^/]+)/?$'] = 'index.php?name=$matches[1]';
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    return $wp_rewrite;
});

还是谢谢你!

【讨论】:

    【解决方案2】:

    更改类别基本前缀

    在更改类别基本前缀后尝试重定向

    【讨论】:

    • 感谢您的帮助,但这并不是我所需要的。所有帖子都必须保留带有日期的url结构,“category-x”帖子除外。
    猜你喜欢
    • 2015-10-11
    • 2018-04-16
    • 2021-08-09
    • 2011-08-15
    • 2011-05-16
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多