Web Design & Development Blog

WordPress is_blog function

By // June 3, 2013 // in PHP, Web Development, Wordpress

I use dynamic sidebars a lot in my website design, and most of the time I have one sidebar for the blog, and one for the rest of the site. I’ve been using this function for a while and it’s pretty dang useful for how simple it is.

This is basically a function for a WordPress web developer/designer to use to check if a page is a ‘blog’ post page. It uses all of WordPress’s functions that return true if the page contains a blog-type query. If the main page query is an archive, author, category, home page, single post, tag, search results, or even the ‘post’ post type, it’ll return false. Awesome!

When I say simple, I mean it!

function is_blog() {

    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 ;

}

Usage:

<?php if (is_blog()) { echo 'This is a blog page!'; } ?>

Let me know if this helps you or if you have any improvements!


Enjoyed this post?

Subscribe to our RSS Feed, follow us on Twitter, or share this article!


By

Brandon has been a web designer and developer since the young age of 10 years old. He has always been devoted to graphic design and creating beautiful website designs, but he also dips his toes in the SEO, web development, and marketing fields. Brandon currently resides at the Wilmington, NC beach.



Subscribe

Subscribe to our newsletter for tips and guides about web design, web development, SEO, and more!

We'll never share your email

Follow us on Twitter

+1 us on GooglePlus

Like us on Facebook

Categories

Archives