is_page(id) not working for blog page

I am trying to add a css file specific to 3 pages using the is_page(postid) for a WordPress website in the functions.php

 if (is_page(1) || is_page(2) || is_page(3) ) {
           echo "<script>console.log('insideIf');</script>";
           wp_enqueue_style( 'custom_css', get_template_directory_child() . path);  
        }

So considering 1,2 and 3 are the ids of the pages, the pages with the ids 2 and 3 are logging the message in the console and loading the css file whereas is_page(1) does not respond.

I tried changing it to is_page('1') or is_single() or is_single(1). The page that is not getting detected is a blog page and i even tried with the title is_page('blog').

Ps. Cross checked the id many times, so I am definitely not using the wrong id.

2 Answers
2

If the page you’re trying to check is set as Page for posts, then is_page conditional won’t be true.

In such case you should check if is_home.

Why? All these conditional tags are based on global wp_query. For page that is set as page for posts (home of your blog), the global query doesn’t contain singular page – it contains list of blog posts.

Leave a Comment