WordPress Conditional Function for Static Home Page vs. Recent Blog Posts
Sometimes you’ll want to test whether or not the home page of a WordPress blog is a static page or the default listing of your most recent blog posts. There is a native way to do this, but it’s a bit cumbersome, so I simplified it by creating a few functions to allow you to easily test for this.
You can add these to your functions.php file:
// test to see if the front page is a blog post page or static page
function home_is_blog_or_static() {
if ( is_front_page() && is_home() ) {
return 'blog';
} elseif ( is_front_page() ) {
return 'static';
}
}
function is_blog_home() {
if ( home_is_blog_or_static() == 'blog' ) {
return true;
} else {
return false;
}
}
function is_static_home() {
if ( home_is_blog_or_static() == 'static' ) {
return true;
} else {
return false;
}
}
Three functions there, though technically you’d only ever need one since it’s a true or false scenario. The first one, home_is_blog_or_static()
, allows you to check for either type. You would use it like so:
if (home_is_blog_or_static() == 'static') { // this is a static page, do what you will } elseif (home_is_blog_or_static() == 'blog') { // this is a blog listings home page, do what you will }
The next two allow you to test for specific types in a more traditional WordPress Conditionals way. Such as:
if (is_blog_home()) { // if this is a home page with blog posts, this will return true }
Or:
if (is_blog_static()) { // if this is a static home page, this will return true }
Up Next: How to Create a Custom Feed in WordPress (without a Plugin)