0

My current situation is:

I'm redirecting the custom category request to below kind of url:

http://my_url/news/category/abc

I've managed to add my custom query var category and can get the query var now. The hook is as below:

add_filter('rewrite_rules_array', function ($rules) {
    return
        ['^news/?$' => 'index.php?post_type=news&page=1&category=all'] +
        ['^news/category/(.+)/?$' => 'index.php?post_type=news&category=$matches[1]'] +
        $rules;
});

Url like http://my_url/news/category/abc now goes to archive-news.php and list all posts which has custom category abc.

And url like http://my_url/news/lalala goes to single-news.php and display full content of post with title lalala.

My problem is:

when url is http://my_url/news/category/ it goes to an empty page which I believe is t he 404.php.

Seek solution

I'm think about how to redirect it to http://my_url/news in my hook so it will automatically picks all as its default category.

All practical solutions are welcome. Thanks in advance.

2
  • "redirect it to http://my_url/news" - a standard redirect will change the URL in the browser's address bar, so, how about adding a rewrite rule for /news/category/, using the same rewrite rule for /news/, i.e. replace the '^news/?$' => with '^news(/category)?/?$' =>.
    – Sally CJ
    Commented May 13, 2022 at 7:32
  • 1
    I tried and it works! Thanks for your advice. All credits to you.
    – ptree
    Commented May 15, 2022 at 23:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.