admin管理员组

文章数量:1201181

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.

Share Improve this question edited May 29, 2019 at 8:46 shea 5,6324 gold badges38 silver badges62 bronze badges asked Jul 19, 2013 at 11:20 KarShoKarSho 6861 gold badge8 silver badges16 bronze badges 0
Add a comment  | 

11 Answers 11

Reset to default 68

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
}

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.

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

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
}

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

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
}

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
}

HOMEPAGE

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

BLOG

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

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.

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.

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>

本文标签: phpCheck if current page is the Blog Page