if ( is_home() && ! is_front_page() )

I see the following code a lot in index.php files. I understand that is_front_page() returns true when viewing the Site Front Page (whether displaying the blog posts index or a static page), while is_home() returns true when viewing the Blog Posts Index (whether displayed on the front page or on a static page). I am still somewhat stumped about the use of the following code -

<?php if ( have_posts() ) : ?>

    <?php if ( is_home() && ! is_front_page() ) : ?>
        <header>
            <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
        </header>
    <?php endif; ?>

Any explanation of why this piece of code is so popular is greatly appreciated.

Solutions

This will display the title of the page when a static page is set to show posts.

E.g.

I show posts on my homepage... It'll do nothing.

If I, say, show posts on page titled News... It'll show News in H1.

This is used so that the title of the page is shown, whenever posts are shown on a page, but nothing when blog posts are shown on the front page (home page).

We do it because if it's on home page... it will show the title of the first post, making it appear twice (once at the top in H1 and again when posts are looped through).

Here is how to do it right:

if ( is_front_page() && is_home() ) {
// Default homepage

} elseif ( is_front_page()){
// Static homepage

} elseif ( is_home()){

// Blog page

} else {

// Everything else

}

This is the only (right) way to display or alter content with your homepage and your blog page.

I am not sure about "popular", it doesn't seem so to me (but then I don't look at that many themes).

You seem to grasp fine what each conditional does, so this shouldn't be confusing to you. This combines conditions to check that blog index is being displayed and it's not at the front page.

Ah, the reason for single_post_title() I would guess is that it displays title for $wp_query->queried object (set up by main query as current context), rather than $post global (set up by iterating loop).

In some circumstances these will be same, but not in such case as condition checks for. The loop will contain posts, but queried object will be page (unless I am mixing things up :).

Tags: Loop / Themes / Templates

Similar questions

WP: is it ok to modify is_front_page() and is_home() functions
I've divided my website to women and men displays, and now I'm worried about the is_front_page() and is_home() functions. Before this change I only had www.site.com to be the front page, now I also have www.site.com/men and www.site.com/women, and I want them to be recognized as front pages as well. I thought about making a new class My_Query exten...
is_home() or is_front_page() check with pagination does not show content of next page
I have an index.php page where I have added the following code to display different content based on the page loaded: In album-grid.php I am using a WP_Query and a pagination. The pagination displays OK. When I click on Next link, it takes me to http://albumlocal.com/page/2 but does not does not show up anything. I am expecting to see the next set ...
Why isn't is_home() working correctly?
I put a if(is_home()) bit in my theme's functions.php and it doesn't seem to work. The function containing this bit is called on init so is_home should work already.
is_home() and is_single() Not Working as Expected with Custom Post Types?
(Moderators note: Was originally titled: "Custom post type problem?") I'm having some problems with custom post types where everything is working great except for the sidebars. Here is some code from my sidebar.php: Normally this works ok except for when I open a single page to check post 'frontpage-sidebar' is not loading as the 'single-post-sideb...
is_home() returns false on the home page! When is the home page NOT the home page?
I've placed the following code in my functions.php file However, no matter what page I'm on, I get "Not Home", And I'm real sure I'm on the home page, but still get "Not Home". What am I missing?
Option to show/hide widget depending on is_home?
Is it possible to add a checkbox to all widgets loaded into sidebars? For example, I'd like to allow the site owner to choose whether any widget should appear on the home page, inside pages, both or neither. I'm thinking a checkbox interface appended to the widget control interface. Has anyone done something like this and what level of complexity a...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.