0

I know there are dozens of posts on this, but (I'm embarassed to say this) I'm having a rough time following them.

My goal here is to have /resources?audience=teachers become /resources/audience/teachers or event /resources/teachers.

I have 2 custom taxonomies on attachments - Types and Audiences. I then have a page titled Resources which is going to house all of the attachments within these categories. If an audience of type is specified, I will only display that term's attachments.

I've tried using the following this within an init hook:

add_rewrite_rule(
    'resources/([^/]+)/?$',
    'index.php?pagename=resources&audience=$matches[1]',
    'top' );

With no success. I'm sure it's a no-brainer.

1

You have to use this:

add_rewrite_rule(
    'resources/audience/([^/]+)/?$',
    'index.php?pagename=resources&audience=$matches[1]',
    'top' );

in your init hook.

Then you should add your own query variable:

function add_my_var($public_query_vars) {
    $public_query_vars[] = 'audience';
    return $public_query_vars;
}
add_filter('query_vars', 'add_my_var');

Then flush rewrite rules (go to permalink settings and push "save" button).

After visiting url: site.com/resources/audience/teachers/ your recources page should be shown. You can get value of audience variable with get_query_var() function.

  • That's great! How would you force /resources/?audience=teachers to show up as /resources/audience/teachers the way /?p=123 redirects to /post-slug ? – Eric Holmes Jul 15 '13 at 19:20
  • Force to show where? You hardcoded this link, I guess, so you have to change it yourself. It isn't link to any existing WordPress object, so WordPress won't generate these links for you. – Krzysiek Dróżdż Jul 16 '13 at 6:47
  • Actually I created it using add_query_arg( 'audience', 'teachers' ) which doesn't handle the rewrite. How would you utilize WP functions to create the rewritten version? – Eric Holmes Jul 16 '13 at 13:50
  • 1
    add_query_arg adds query argument (tada ;)). If you don't wont to add it as query argument and wat to create link in some format, just do it ;) (append it to the link) – Krzysiek Dróżdż Jul 16 '13 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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