Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress is_blog()
function is_blog () {
global $post;
$posttype = get_post_type($post );
return ( ((is_archive()) || (is_author()) || (is_category()) || (is_home()) || (is_single()) || (is_tag())) && ( $posttype == 'post') ) ? true : false ;
}
Usage:
<?php if (is_blog()) { echo 'You are on a blog page'; } ?>
@gavinsmith

This comment has been minimized.

Copy link

gavinsmith commented Sep 2, 2011

The only stubborn component currently difficult to check for consistently (across all installs) is custom post type indexes.

@wesbos

This comment has been minimized.

Copy link
Owner Author

wesbos commented Sep 2, 2011

@gavinsmith Ah yeah, does is_archive() trigger on CPT archive?

@wesbos

This comment has been minimized.

Copy link
Owner Author

wesbos commented Sep 6, 2011

Updated it to ignore custom post types

@eggfriedrice

This comment has been minimized.

Copy link

eggfriedrice commented Oct 23, 2011

Thanks for this function, exactly what I needed today. I can't believe it's not a builtin!

@lekkerduidelijk

This comment has been minimized.

Copy link

lekkerduidelijk commented Nov 16, 2011

Thanks @wesbos!

@grantnorwood

This comment has been minimized.

Copy link

grantnorwood commented Aug 17, 2012

Very helpful, thanks! However, the is_archive() function covers the is_post_type_archive(), is_date(), is_author(), is_category(), is_tag(), and is_tax() functions, so you can safely remove them. See http://core.trac.wordpress.org/browser/tags/3.4.1/wp-includes/query.php#L1572.

The revised function might look like:



/**
 * WordPress' missing is_blog_page() function.  Determines if the currently viewed page is
 * one of the blog pages, including the blog home page, archive, category/tag, author, or single
 * post pages.
 *
 * @return bool
 */
function is_blog_page() {

    global $post;

    //Post type must be 'post'.
    $post_type = get_post_type($post);

    //Check all blog-related conditional tags, as well as the current post type, 
    //to determine if we're viewing a blog page.
    return (
        ( is_home() || is_archive() || is_single() )
        && ($post_type == 'post')
    ) ? true : false ;

}
@thefuxia

This comment has been minimized.

Copy link

thefuxia commented Oct 12, 2012

The ? true : false at the end is not needed. The function will return boolean anyway.

@jacobdubail

This comment has been minimized.

Copy link

jacobdubail commented Nov 16, 2012

Does this function register early enough to use it in a pre_get_posts action?

@abouolia

This comment has been minimized.

Copy link

abouolia commented May 3, 2014

is_archive! but what about is_search() ?! even this contains on blog posts

@joshuaanderton

This comment has been minimized.

Copy link

joshuaanderton commented Jun 29, 2015

Awesome. So useful, thanks!

@codename065

This comment has been minimized.

Copy link

codename065 commented Aug 10, 2015

How about this:

function is_blog(){
        if ( is_front_page() && is_home() ) {
            return false;
        } elseif ( is_front_page() ) {
            return false;
        } elseif ( is_home() ) {
            return get_option( 'page_for_posts' ); // Returns blog page ID
        } else {
            return false;
        }
    }

Ref: https://codex.wordpress.org/Conditional_Tags#The_Blog_Page

@jancbeck

This comment has been minimized.

Copy link

jancbeck commented Nov 13, 2015

Less verbose:

function is_blog() {
    return ( is_author() || is_category() || is_tag() || is_date() || is_home() || is_single() ) && 'post' == get_post_type();
}

I removed is_archive because I am using custom post types and taxonomies.

@AlbertParera

This comment has been minimized.

Copy link

AlbertParera commented May 22, 2017

How about this?
in fact, is_singular() is the same as if ( is_single() || is_page() || is_attachment() )

if ( is_singular( 'post' ) ) {
	echo 'You are on a blog page'; 
}
@philtune

This comment has been minimized.

Copy link

philtune commented Sep 5, 2018

Taking @grantnorwood's code from 2012:

  • The ternary return is unnecessary so let's just return the comparisons.
  • Also, $post_type == 'post' is the cheaper operation so let's put it first.
  • Finally using triple equals as there should be no type conversion.
/**
 * WordPress' missing is_blog_page() function.  Determines if the currently viewed page is
 * one of the blog pages, including the blog home page, archive, category/tag, author, or single
 * post pages.
 *
 * @return bool
 */
function is_blog_page()
{
    global $post;

    // Post type must be 'post'.
    $post_type = get_post_type($post);

    // Check all blog-related conditional tags, as well as the current post type, 
    // to determine if we're viewing a blog page.
    return ( $post_type === 'post' ) && ( is_home() || is_archive() || is_single() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.