1

I'm trying to figure out how I can change the page that loads. Let's assume I'm just using the default permalink and I have a URL with a page_id. I want to be able to override the page that would normally load and load a different page based on some logic that I will maintain.

I've dug through a bunch of the files and I cannot seem to figure out where I can catch the page_id and override it.

I will admit I'm not all that experienced with WP development so I'm not really sure what I am doing. I'm just looking for some direction here to make sure this is even possible.

Thanks in advance for any help.

2
  • 1
    can you edit the question and specify the conditions you are interested in? Otherwise there are actually too many ways to do that to list them all. Commented Feb 24, 2015 at 3:49
  • I've posted an answer but it may be off base. If so, please give more information and I'll try and help further.
    – David Gard
    Commented Feb 24, 2015 at 13:56

2 Answers 2

1

Wp load pages based on it's main query.

SO if you want to load a different page, then you may have to alter the main query with th e pre_get_posts hook.If you want to know more.Then please specif what and how you want to load different page in details

1

Your question isn't particularly clear I'm afraid, but it sounds to me like you are trying to override the default template. For example, you still want the contents of your page to be displayed, but you don't want to use the default page.php template.

If I am correct (please let me know in the comments if not), then there are two ways to do this.


Override using the Template Hierarchy

If, for example, you have only one page that you wish to display using a different template you can simply name that page-$slug.php or page-$id.php.

For example, if you have a page called Example Page with a slug of example and an ID of 10, you could create a template called either page-example.php or page-10.php.

For more information on this please see the Codex for the WordPress Template Hierarchy.


Override using the 'template_include' filter

If the first method is no good for you there is another way.

Using the 'template_include' filter you can check which page is about to be displayed and, if you desire, change the template that will be used.

For more information, please see the Filter Reference for template_include.

add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {

    if ( is_page( 'portfolio' )  ) {
        $new_template = locate_template( array( 'portfolio-page-template.php' ) );
        if ( '' != $new_template ) {
            return $new_template ;
        }
    }

    return $template;
}

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.