1

I have been studying WordPress theme developer handbook. And I got stuck understanding the difference between is_page and is_page_templates. Can someone help me to get a clear understanding easily?

2 Answers 2

7

is_page() returns true if the main query is for a single Page. If you provide an argument, which can be an ID, slug, or title, it will only return true if the main query is for the specific page with that ID, slug, or title.

is_page_template() returns true if you are viewing a page that has the given Page Template.

So is_page_template( 'template-about.php' ); will return true if you are viewing a page that is using the custom template template-about.php. While is_page( 'about' ) will only return true of you are viewing a page with the slug about.

Regarding when I say 'the main query', whenever you visit a URL in WordPress there will be a query for the content for that URL. So on http://example.com/about the main query will be for the page about, while on http://example.com/category/tutorials the main query will be for posts in the tutorials category. So is_page() is checking what the main thing being queried is.

4

The WordPress function is_page_template() is used to determine if the user is on a page assigned to a specified page template.

I use it often when I enqueue styles and scripts, like so:

if ( is_page_template( 'page-templates/flex-page.php' ) ) {
     wp_enqueue_script( 
        'event-move', 
        get_template_directory_uri() . '/assets/js/jquery.event.move.js', 
        array('jquery'), 
        '01-22-2018', 
        true 
    ); 
}

The WordPress function is_page() is more specific : it asks if the user is on a specific (singular) page. The parameters that can be passed to the function is usually the page id, but can be page ID, title, slug or array.

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.