0

I am really not sure how / what to ask, but ill try my best...

Can I some how tell Wordpress (in the index.php file maybe):

If A Then load page Y with his matching template (which you always know so well which is it) and if B load page Y with his matching template (which...)?

I mean something like maybe setting the front page by my condition... let wordpress then set the query and the right template and everything.

Another way to say it is:

I want to tell Wordpress: you know if i was setting the front page (through the admin settings -> reading panel) to page titled "page1" you would have show it on the front page handling the page data, template and everything needed on the main url. now i want you to do the same but if something happens (let's say the user is logged in or no matter what kind of and "if" condition is running) do it with another page titled "page13" and if another thing happens do it with another page titles "page5"...

2 Answers 2

0

I'm not sure this is a great way, but for now that's what I have:

  1. Set the front page setting in WordPress to posts and not static page, so WordPress will use the index.php file.

  2. In the index.php file write:

if (something) {
    global $post;
    $post = get_post( "the post id" );
    setup_postdata( $post );
    include('page-template-file.php');
}
else {
    global $post;
    $post = get_post( "the other post id" );
    setup_postdata( $post );
    include('page-other-template-file.php');
}

Note: you have to deal other pages with singular.php template and so... otherwise with this solution you will load one of this pages for any page on your site.

2
  • I do have at least one problem with that solution: when i load a page (not a post) in that method. is_page() condition wont work in the header.
    – Nori
    Commented Jun 21, 2017 at 9:34
  • you can fix that is_page problem by using $post->ID == the post id.
    – Nori
    Commented Jun 21, 2017 at 9:49
0

To overwrite the default template you can use the template_include hook, which could be used like this:

add_filter('template_include', 'my_custom_template');
function my_custom_template($original_template) {

    if ($something) {
        return include_wordpress_template(get_template_directory() . $my_custom_template);
    } else {
        return $original_template;
    }
}

You can find more info about the hook here.

UPDATE

To change the data retrieved you can change the query using the pre_get_posts hook for it like this:

function my_pre_get_posts($query) {
    global $post;

    if ($query->is_main_query() && $your_condition) {
        $query->set( 'post_type', 'your_post_type' );
        $query->set( 'p', $some_special_id );
    } else {
       return $query;
    }
}
add_action( 'pre_get_posts', 'my_pre_get_posts' );
6
  • Thanks @Picard, but this won't load a specific page as i asked.
    – Nori
    Commented Jun 21, 2017 at 3:46
  • How do you define a "page"? A specific template for the default data or a template plus post data of your choice?
    – Picard
    Commented Jun 21, 2017 at 6:00
  • I guess. my goal is to get what the front page setting is getting. set the specific page / post that will be loaded on the first boot of Wordpress on the main domain (Without changing the url).
    – Nori
    Commented Jun 21, 2017 at 6:43
  • If I understand you correctly - if you want to use the provided data but in a custom template, just check what data you are getting - add global $post; at the top of the my_custom_template function and use it to alter $something condition and the $my_custom_template variable.
    – Picard
    Commented Jun 21, 2017 at 6:58
  • I don't want to use the provided data. i want to provide data and that Wordpress will follow. i want to tell Wordpress: you know if i was setting the front page (through the admin settings -> reading panel) to page titled "page1" you would have show it on the front page handling the page data, template and everything needed on the main url. now i want you to do the same but if something happens (let's say the user is logged in or no matter what kind of and "if" condition is running) do it with another page titled "page13" and if another thing happens do it with another page titles "page5"...
    – Nori
    Commented Jun 21, 2017 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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