51

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.

0

11 Answers 11

67

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() and is_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
}
2
  • One use case to use if ( !is_front_page() && is_home() ), at least in my opinion, is if you are distributing a theme that has different layout style for the default homepage and the blog page.
    – Giraldi
    Aug 3, 2014 at 7:03
  • I'm finding is_front_page() will return true whether or not the blog archive or a page is selected. Need verification. codex.wordpress.org/Function_Reference/is_front_page
    – atwellpub
    May 28, 2017 at 0:32
30

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.

2
  • 2
    This is a great answer if you want to determine if you're on a blog page, but not neccessarily the blog page (as in the blog home page). See @Giraldi's answer for that.
    – Tim Malone
    Apr 17, 2016 at 22:04
  • I incorrectly assumed is_blog() exists because is_page() exists. It didn't occur to me to consult the official WordPress Conditional Tags Index. I was able to effectively apply this solution using the Widget Logic plugin. Apr 14, 2019 at 12:34
18

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

2
  • Hi repinsa, welcome to WPSE :) Thanks for adding your answer. It's been voted down a bit, probably because it has a syntax error in the code (it's missing a semicolon after the global $wp_query) but also because it doesn't fully answer the question. It's a function, but the OP asked how to work this out in his header file - so it might need a little more explanation about what to put where. Again, welcome, glad to have you here!
    – Tim Malone
    Apr 17, 2016 at 22:07
  • 3
    That's actually the only good answer here, should have had more upvotes.
    – LachoTomov
    Dec 23, 2017 at 14:16
7

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
}
0
1

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

2
  • 2
    Doesn't work if you've changed the blog page
    – cdmckay
    Oct 9, 2014 at 15:09
  • This doesn't provide a correct answer to the OP. This indicates you are on a single post, not "the blog page".
    – butlerblog
    Dec 27, 2017 at 14:29
0

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
}
2
  • This is not ideal, I wouldn't recommend it. Sep 28, 2021 at 22:40
  • I wouldn't rely on a CSS class.
    – Azamat
    Aug 31, 2022 at 5:13
0

There is a tricky method.

http://example.com/blog

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
}
0

HOMEPAGE

if(is_home() && is_front_page() || is_front_page()): // static or default hompage
 ...
endif;

BLOG

if(is_home() && !is_front_page()): // blog
 ...
endif;
0

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.

0

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.

0

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'] enter image description here

   <li <?php if(get_post_type() == 'post' )
     {echo 'class="current-menu-item"';} ?> >
       <a href="<?php echo site_url('/blog');?>">
         Blog
       </a>
    </li>

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.