Image Attachment Url Rewrite

I’m having an issue with url rewriting for a theme I’m making, which is basically a gallery theme based on the roots theme. Here’s how the site navigation works:

  • Home Page (With Tiled Image Gallery) (Images link to parent pages) Current URL – domain.com
  • Parent Page (With Tiled Image Gallery) (Images link to attachment page along with #id to link to the clicked image ). Current URL – domain.com/landscapes
  • Attachment Page (With ALL full size images attached to the parent post). Current URL – domain.com/landscapes/image-title/#id

What I would like to do is to remove the image title from the attachment page URL and replace it with gallery so that the URL reads domain.com/landscapes/gallery/#id

Read More

My content-image.php uses wp_get_attachment_image_src() to get the image urls. With pretty permalinks disabled i get an image URL like domain.com/index.php?attachment_id=205

When checking with pretty URL with the Rewrite Rules Inspector Plugin, that URL (domain.com/landscapes/image-title/#id) gets 2 matches which are as follows:

RULE 1: (.?.+?)(/[0-9]+)?/?$

Rewrite 1: index.php?pagename=$matches[1]&page=$matches[2]

RULE 2: [^/]+/([^/]+)/?$

Rewrite 2: index.php?attachment=$matches[1]

From what I understand, The second rule is the one that is being applied in this case, but I have no clue what to change it to. Any idea on how to do this?

Related posts

1 comment

  1. Ok so I found the answer. I used the code as per https://wordpress.stackexchange.com/a/56426/37472 and changed all references of 'series' to 'gallery'.

    That added the required rewritre rule to parse a link like domain.com/landscapes/gallery/123/ to index.php?attachment_id=123.

    My assumption was that wordpress would display the default permalink that is generated by get_permalink() in the domain.com/landscapes/gallery/123/ format. It does not. However, I used a simple workaround in my gallery script to generate the anchor links in the required format, which is different for the home page, sub pages and parent/other pages. The script uses isotope and lazy load to load the galleries http://pastie.org/8294032.

    For the different format of the pages, i modified the my_rewrite_rules_array( $rules ) function as follows:

    function my_rewrite_rules_array( $rules ) {
        $my_rules = array();
        $my_rules['gallery/(d+)/?$'] = 'index.php?attachment_id=$matches[1]';
        $my_rules['(w+)/gallery/(d+)/?$'] = 'index.php?attachment_id=$matches[2]';
        $my_rules['(w+)/(w+)/gallery/(d+)/?$'] = 'index.php?attachment_id=$matches[3]';
        return $my_rules + $rules;
    }
    

Comments are closed.