30

I'm new to Wordpress. I need to check in header file if current page is the Blog Page?

I checked but I can't find the way. Help me, Pls.

38

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
}
  • 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 '14 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 '17 at 0:32
16

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.

  • 1
    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 '16 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. – Clarus Dignus Apr 14 at 12:34
9

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

  • 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 '16 at 22:07
  • 2
    That's actually the only good answer here, should have had more upvotes. – Lacho Tomov Dec 23 '17 at 14:16
2

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

You can use

<?php if ( is_single() ) { ?>

Do stuff here

<?php } ?>
  • 1
    Doesn't work if you've changed the blog page – cdmckay Oct 9 '14 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 '17 at 14:29
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.

-1

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.