Is it possible to list post attachments in a sub URL endpoint with a dedicated template?

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.

Read More

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/

http://codex.wordpress.org/User:DavidHouse/WP_Rewrite_API

URL Design for Sub-Posts?

http://codex.wordpress.org/Template_Hierarchy

Related posts

Leave a Reply

1 comment

  1. I think I found it myself. Now I’ve successfully managed to add a /photos endpoint to a permalink URL, such as:

    http://mywordpressite.com/projects/a-project/photos
    

    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.