Check if current page is the Blog Page
I'm new to WordPress. I am looking for a way to check if current page is the blog page in the code of the header file.
I've checked but I can't find a way. Help me, Pls.
I'm new to WordPress. I am looking for a way to check if current page is the blog page in the code of the header file.
I've checked but I can't find a way. Help me, Pls.
This worked for me... Even if I go inside individual posts blog menu is activated. With this, you can always target the blog page.
[Note: This will work when if your blog is the 'Posts Page']

   <li <?php if(get_post_type() == 'post' )
     {echo 'class="current-menu-item"';} ?> >
       <a href="<?php echo site_url('/blog');?>">
         Blog
       </a>
    </li>
In my opinion, the best solution instead of checking if the page is home or archive or not OR & AND you can simply check the template you use.
For example: I use in my blog posts page this template:
template-blog.php
So, I can distinguish it from any other page as follow:
if( is_page_template('template-blog.php') ) {}
Hope this help.
You can use..
<?php if ( is_single() ) { ?>
Do stuff here
<?php } ?>
to check if it's a single blog post. Or...
<?php if ( is_home() ) { ?>
Do stuff here
<?php } ?>
to check if it's the blog homepage
To get the blog index page, I found that
if ( !is_front_page() && is_home() ) {
  // blog page
}
is not working for me, I had to use the get_option('page_for_posts') function to identify the Blog Page post_id, my answer is
if ( !is_front_page() && is_home() ){  
    if ( empty ( $post_id) ) {
        global $post;
        $post_id =  get_option( 'page_for_posts' );
    }
    //blog page
}
I guess its very simple I was in a same situation and I used the following technique which is to use the page slug.
if( is_page('blog') ) {
echo "This is your blog page"; 
}
But make sure you've not selected homepage to display recent blog posts and you have set a specific page for blogs like blog or news etc, just use that page slug and you'd be fine.
HOMEPAGE
if(is_home() && is_front_page() || is_front_page()): // static or default hompage
 ...
endif;
BLOG
if(is_home() && !is_front_page()): // blog
 ...
endif;
There is a tricky method.
Suppose if your blog page slug is blog, you can use this code.
global $wp_query;
if($wp_query->query['pagename']=='blog'){
// this is blog page
}
If by 'blog page' you meant a static page set as posts page in the Reading:
global $wp_query;
if ( isset( $wp_query ) && (bool) $wp_query->is_posts_page ) {
    //static blog page
}
PS. This solution also works on template_redirect action
You can use the following in your themes functions.php file:
function is_blog () {
    return ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag()) && 'post' == get_post_type();
}
And then put this in the file you are checking:
<?php if (is_blog()) { echo 'You are on a blog page'; } ?>
You can use Hooks in your functions.php file to hook the above, to make that appear on every page.
I use this way
// Get body classes as array
$body_classes = get_body_class();
// Check if "blog" class exists in the array
if(in_array("blog", $body_classes)) {
   // Do stuff
}
If by 'blog page' you meant a static page set as posts page in the Reading Settings, then you could check it by doing this:
if ( is_front_page() && is_home() ) {
  // Default homepage
} elseif ( is_front_page() ) {
  // static homepage
} elseif ( is_home() ) {
  // blog page
} else {
  //everyting else
}
When you use
is_home()andis_front_page(), you have to use them in the right order to avoid bugs and to test every user configuration.
(Source: Conditional Tags - The Blog Page)
Or simply:
if ( !is_front_page() && is_home() ) {
  // blog page
}
Or more simply (I suppose):
if ( is_home() ) {
  // blog page
}
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.