Check if page is a post or blog page in WordPress

No Comments

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:

if (is_blog()) { echo 'This is a blog page!'; }

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

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.

 

About us and this blog

We are a full service agency that deliver compelling digital marketing solution. Our winning solutions and experience helps to deliver great results across several key areas.

Request a free quote

We offers professional SEO services that help websites increase organic search drastically and compete for 1st page rankings of highly competitive keywords.

20+

sidebar widgets

More from our blog

See all posts
 
Add Comment Register



Leave a Comment