WordPress provides templates for displaying media attached to a post or page (attachment.php
and more). They’re also displayed with nice permalinks, such as:
site.com/projects/a-project/an-attachment/
Where projects
is the slug for my custom post type project
. a-project
is the post name, and an-attachment
is the name of an attached image.
The functionality I’m asking for is a sub URL endpoint for listing all post attachments with a dedicated template file. It would be great with a permalink for a post’s attachments, wouldn’t it?
Such as:
site.com/projects/a-project/attachments/
or
site.com/projects/a-project/images/
Is it possible? If not with pretty permalinks, is it with some kind of query variable? Apart from showing a single attachment, I haven’t ever seen another form of sub URL endpoint to a WordPress post before.
Thanks in advance!
EDIT
Some interesting resources:
http://johnbeales.com/20090824/endpoints-a-little-secret-for-url-manipulation-in-wordpress/
http://wordpress.org/support/topic/custom-rewrite-approach-with-add_rewrite_endpoint
http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/
I think I found it myself. Now I’ve successfully managed to add a
/photos
endpoint to a permalink URL, such as:And then a custom template,
photos.php
will load and show â with access to the global$post
variable.Helpful links:
In my
functions.php
I added functions for thesingle_template
andquery_vars
filters and added aphotos
endpoint with the handyadd_rewrite_endpoint()
function.Further down in the file:
Be sure to visit the Permalink page in wp-admin for the changes to take effect.