7

I am trying to include a special Archive page with my theme, right now I have a PHP file with

Template Name: Archives Template at the top and I the user must create a blank archive POST PAGE and then select this as the template.

The content for creating the page is blank so nothing is added into the database for the page content. Is there anyway to make it where they do not have to do this?

  • Are you trying to get an archive for a custom post type? – Brian Fegter Jan 1 '12 at 6:26
  • No archive was just used for an example, I am trying to add a page to a theme without making the user add a page to the DB – JasonDavis Jan 1 '12 at 7:49
13

To wit, this technically isn't a 'page', however it's a way to surface content using mod_rewrite. The goal here is to add a URI segment of foobar that will trigger a custom query, then include the specified template file.

Add a rewrite rule:

add_action('init', 'foo_add_rewrite_rule');
function foo_add_rewrite_rule(){
    add_rewrite_rule('^foobar?','index.php?is_foobar_page=1&post_type=custom_post_type','top');
    //Customize this query string - keep is_foobar_page=1 intact
}

See the WP_Query documentation for more information about customizing the query string.

Register a new query var:

add_action('query_vars','foo_set_query_var');
function foo_set_query_var($vars) {
    array_push($vars, 'is_foobar_page');
    return $vars;
}

Force WP to select your page template:

add_filter('template_include', 'foo_include_template', 1000, 1);
function foo_include_template($template){
    if(get_query_var('is_foobar_page')){
        $new_template = WP_CONTENT_DIR.'/themes/your-theme/template-name.php';
        if(file_exists($new_template))
            $template = $new_template;
    }
    return $template;
}

Flush the rewrites by visiting Settings->Permalinks, then visit http://yourdomain.com/foobar

| improve this answer | |
  • :) Glad to help. – Brian Fegter Jan 4 '12 at 2:01
  • I've tried this and it works, but one issue I'm having is that it ends up being a front page/ home page and I'd like it to be a 'single'. Know of any way of adjusting this? – Adam Christianson May 18 '17 at 23:13
2

An additional remark to Brian Fegter's answer. if you want to flush the rewrites rules programmatically instead of visiting Settings->Permalinks, you can use flush_rewrite_rules().

However, don't put it on init hook, as the Wordpress Codex strongly recommands. You can put both flush_rewrite_rules() method and the foo_add_rewrite_rule() on register_activation_hook.

register_activation_hook( __FILE__, 'myplugin_flush_rewrites' );
function myplugin_flush_rewrites() {
add_rewrite_rule('^foobar?','index.php?is_foobar_page=1&post_type=custom_post_type','top');
flush_rewrite_rules();
}
| improve this answer | |
1

I was able to stop the is_front_page() and is_home() functions from evaluating to true for my "fake" page by adding a pre_get_posts method

add_action( 'pre_get_posts', 'foo_pre_get_posts', 1 );
function foo_pre_get_posts( $query ) {
    // check if the user is requesting an admin page 
    // or current query is not the main query
    if ( is_admin() || ! $query->is_main_query() ){
        return;
    }

    // edit the query only when we are dealing with our "fake" pages
    // if it isn't, return
    if ( get_query_var('is_foobar_page') ) {
        $query->set('post_type', 'page');
        $query->is_home = false;
    }

}
| improve this answer | |

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.