Get link to a specific template file

I was planning on creating a custom “Archives” template to create a detailed Archives page for my blog, including lists of posts by category, tag, date and author. Rather than use a static link though, is there a way within WordPress to get a link to a specific template file? I want the link to work regardless of the page’s URL—is this possible?

Related posts

Leave a Reply

2 comments

  1. Here’s a function to print the URI of the first page using an “Archive” page template. It assumes the template is named template-archive.php (don’t name it archive.php and hijack the Template Hierarchy!). You can either drop this in functions.php and call it in a template or you can just use the code within the function directly in a template file.

    function wpse25797_the_archives_link() {
        $archive_page = get_pages(
            array(
                'meta_key' => '_wp_page_template',
                'meta_value' => 'template-archive.php'
            )
        );
        $archive_id = $showcase_page[0]->ID;
        echo get_permalink( $archive_id );
    }
    
  2. Depends on what its an archive of. Take a look at the Template Hierarchy…

    http://codex.wordpress.org/images/1/18/Template_Hierarchy.png

    As you can see there are many kinds of archives. Taxonomy, Category, Tag, Author, Date, and Posts. If no template of the specific type is found when on an ‘archive’ query, then WordPress defaults to a generic ‘archive.php’ template. If thats not found either, it will default to ‘index.php’.

    If you are trying to make a general archive page that shows several kinds of archives combined, you could theoretically do that anywhere in the hierarchy – the questions to answer then would be where and how you want that part of your site to show up – and what kind of data will be shown there (is it relative to the cat your on, or is it a central generic type of thing).

    For example if you create a template called category.php, then any category request will show up using that template, unless you create a specific one for a particular category. So if you have category.php and category-myslug.php, then the ‘myslug’ category will show up with its special template, but all others will show up using the general category.php template.

    To bring this all home, if your url is http://example.com/category/cat1, it can be made to use the same template as http://example.com/category/cat2 based on which templates do and done exist within your theme.

    The same logic applies to tag templates, page templates, date-based archives, custom taxonomy templates, and custom post type templates. Additionally, if you want a single template to apply to ALL archives, just create an archive.php template and make sure you dont have one of the more specific ones in your theme.

    Hope this helps.