0

I would like to get this:

mysite.com/category/tag/

example:

mysite.com/news/sports/

News is a CATEGORY and sports is a TAG

How can I do it?

Thanks.

2
  • And on this page /news/sports/, the only posts that would show would be posts categorized news and tagged sports? Feb 23, 2013 at 20:56
  • Yes. It is what I want.
    – Mike
    Feb 23, 2013 at 21:12

1 Answer 1

1

WordPress is practically setup to support this out of the box. You can go to mysite.com/?category_name=news&tag=sports to see the magic happen. All we need to do here is match a permalink structure to this existing URI, and WP makes this unreasonably easy.

In your theme's functions.php file, add:

add_action( 'init', 'wpse_88059_add_rewrites' );
function wpse_88059_add_rewrites() {
    add_permastruct( 'category_tags', '%category%/%post_tag%', array( 'walk_dirs' => false ) );
}

Now be sure to reload your rewrite rules by going to Settings -> Permalinks and clicking "Save Changes" (you don't have to actually change anything) and you're done!

Caveats

This is going to add some "wildcard" rewrite rules which can easily interfere with others, depending on your site's content, permalink structure, etc. For instance, let's say your permalink structure is "Day and name", which adds rules for year/month archive pages like /2013/02/. These links will no longer work because WordPress is expecting to find a category of "2013" and a tag of "02".

This is why WordPress typically prefixes elements, like /category/news/. Depending on your needs, these conflicts may be a dealbreaker or may be moot, that's up to you and your project. If you want to add a static prefix (or suffix), you could change %category%/%post_tag% to something like archives/%category%/%post_tag% to get URIs like /archives/news/sports/ or %category%/%post_tag%/articles to get URIs like /news/sports/articles/.

8
  • It doesn't work when I go into a post. By other hand, If I want mysite.com/news/2010, how can I get it?
    – Mike
    Feb 23, 2013 at 21:55
  • The easiest way would be to add a static prefix or suffix as I outlined above. Feb 23, 2013 at 21:56
  • It doesn't work when I go into a post, because is expecting to find a tag with the title of the post :(
    – Mike
    Feb 23, 2013 at 22:02
  • If your current permalink structure is %category%/%postname%, then what you're asking in your question fundamentally isn't going to work for you. Feb 23, 2013 at 22:05
  • My structure is %category%/%postname%. Is not a way to get it?
    – Mike
    Feb 23, 2013 at 22:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.