URL rewrite with add_rewrite_rule and attachment_id

I’m trying to add a rewrite rule to make this:

mysite/?attachment_id=106

look like this:

mysite/series/106

I’ve looked everywhere in this site and others and it’s very confusing because there are lots of different ways to do it.
I’ve tried editing the functions.php file in my themes folder, adding this at the beginning of the file:

add_rewrite_rule('series/([^/]+)/?$', 'index.php?attachment_id=$matches[1]', 'top');

And the latest one I’ve tried:

add_filter( 'query_vars', 'wpa56345_query_vars' );

function wpa56345_query_vars( $query_vars ){
    $query_vars[] = 'attachment_id';
    return $query_vars;
}

add_action( 'init', 'wpa56345_rewrites' );

function wpa56345_rewrites(){
    add_rewrite_rule(
        'series/(\d+)/?$',
        'index.php?attachment_id=$matches[1]',
        'top'
    );
}

2 Answers
2

Place the code suggested below in functions.php of your desired theme, it worked for me with WP 3.4: http://matty.co.za/2009/11/custom-url-rewrites-in-wordpress/

Leave a Comment