0

We work with non-profits and come across this issue a lot. We need to use a page template for the url structure domain.com/2015 where we'll be adding an "end of year" report.

We're not using the typical date archives to list blog posts. I'm not sure how I'd redirect the template based on this.

My question is, how can I force the use of a page template instead of date.php or archive.php?

  • What have you tried so far? Are you only looking to change the 2015 or all is_date() archives? have you looked at template_rediriect hook? What do you mean by "We're not using typical date archives" exactly? – Howdy_McGee Nov 25 '15 at 18:15
  • I actually was filtering the $template right. I was locating the template incorrectly. It was nested in a folder and that needed to be included in the array(). – Bryan Monzon Nov 25 '15 at 18:41
  • The only issue with this is that you're still needlessly running the date archive query even though you don't use the results. You could change the date archive structure to prevent this and add your own rewrite rule. You would have to also filter wp_unique_post_slug if you wanted to name pages with a year, WordPress would still erroneously think it conflicted with a date archive. – Milo Nov 25 '15 at 18:46
1

If you really want to override the yearly archive for 2015, then you can try the following:

add_filter( 'template_include', function( $template ) 
{
    //-------------------------
    // Edit this to your needs:
    //-------------------------
    $year = 2015; 
    $report_template = "tpl-annual-report-{$year}.php";

    //----------------------------------------------
    // Override the default yearly archive template
    //----------------------------------------------
    if ( 
           is_year()                          // Yearly archive
        && $year == get_query_var( 'year' )   // Target the year 2015 
        && $new_template = locate_template( [ $report_template ] ) 
    )
        $template = $new_template ;

    return $template;
} );

where your annual report template for the year 2015 is tpl-annual-report-2015.php.

More on the template_include filter in the Codex here.

  • you can also use the more specific date_template filter so your code only runs on date archive requests. – Milo Nov 25 '15 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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