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:


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:


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!


Some interesting resources:





URL Design for Sub-Posts?



1 Answer 1


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 the single_template and query_vars filters and added a photos endpoint with the handy add_rewrite_endpoint() function.

// Permalink rewrites/add endpoints
add_filter( 'single_template', 'project_attachments_template' );
add_filter( 'query_vars', 'add_query_vars');

// You may need to go to wp-admin > Settings > Permalinks and 
// save the changes in order to flush rewrite rules to activate.
add_rewrite_endpoint('photos', EP_PERMALINK);

Further down in the file:

*   Add the 'photos' query variable so Wordpress
*   won't mangle it.
function add_query_vars($vars){
    $vars[] = "photos";
    return $vars;

*   From http://codex.wordpress.org/Template_Hierarchy
*   Adds a custom template to the query queue.
function project_attachments_template($templates = ""){
    global $wp_query;

    // If the 'photos' endpoint isn't appended to the URL,
    // don't do anything and return
    if(!isset( $wp_query->query['photos'] ))
        return $templates;

    // .. otherwise, go ahead and add the 'photos.php' template
    // instead of 'single-{$type}.php'.
    if(!is_array($templates) && !empty($templates)) {
        $templates = locate_template(array("photos.php", $templates),false);
    elseif(empty($templates)) {
        $templates = locate_template("photos.php",false);
    else {
        $new_template = locate_template(array("photos.php"));
        if(!empty($new_template)) array_unshift($templates,$new_template);

    return $templates;

Be sure to visit the Permalink page in wp-admin for the changes to take effect.

  • This worked really well for me as a mu-plugin, except I had to hook the add_rewrite_endpoint function into init. Thanks for sharing the code! Jan 11, 2013 at 21:36

Your Answer

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

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