Created
September 2, 2011 19:32
-
-
Save wesbos/1189639 to your computer and use it in GitHub Desktop.
WordPress is_blog()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; } ?> |
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';
}
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() );
}
Has anyone noticed that this fails when there are no posts assigned to a tag or category? $post is NULL when you visit a category/tag archive that has no posts to display. I've been using this for years and seen it on and off - i mostly use this just for page titles, which weren't showing in this case. I never looked into it because usually the categories/tags fill up with posts and it's not an issue! But clients sometimes...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Less verbose:
I removed
is_archive
because I am using custom post types and taxonomies.