1

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?

1
  • Do I read your question correctly that you're thinking of a page template selected in the "Template" field?
    – mrwweb
    May 2, 2012 at 17:25

2 Answers 2

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 );
}
0

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.

4
  • I wish the WordPress terminology were clearer, but if you reread the question, I believe this is about a custom "Archive" Page Template and not the archive.php or related template files.
    – mrwweb
    May 2, 2012 at 17:53
  • He said template, and I am in fact talking about archives. There are many kinds of archives, thats what I was explaining. Theres no way to make a 'custom archive template' without it involving, well, a template.
    – eddiemoya
    May 2, 2012 at 18:17
  • Right, but I believe he's talking about this kind of template (codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates) which is pretty much outside of the template hierarchy.
    – mrwweb
    May 2, 2012 at 18:44
  • You might be right about that, but thats not entirely clear from the post - however thats still a template - and if nothing else he should consider using something already made available in the hierarchy. With static pages/template like you mentioned theres really no simple built-in way to make something work dynamically for any archive query (like for any category, or for any post archive, etc), thats what the hierarchy allows you to do. Thats what I think he's asking for when he says it should work regardless of the URL. /category/cat and category/cat2 should both work.
    – eddiemoya
    May 2, 2012 at 18:59

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