public
Last active

WordPress is_blog()

  • Download Gist
is_blog.php
PHP
1 2 3 4 5 6 7 8 9 10
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'; } ?>

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

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

Updated it to ignore custom post types

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

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 ;

}

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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.