39

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.

1

3 Answers 3

21

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).

2
  • Thank you Shramee. I found the title of my blog index page in a <h1> when trying to figure it out but it doesn't actually appear anywhere. I think some themers use it only to help people using screen-readers, and then hide the actual text, which is a bit confusing for newbies if its not commented/documented.
    – olliew
    Commented Apr 23, 2016 at 13:53
  • Well... It helps to know in which context posts are used, (like in above example for News) but it's mainly there for SEO, H1 plays a big role in helping search bots understand the page content better.
    – shramee
    Commented Apr 24, 2016 at 6:16
56

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.

1
  • So is this how you style different pages in wordpress? For example my homepage may have a big hero image but the About and Contact pages may just have not have one at all. Is this how you go about doing different layout for different pages? Commented Feb 23, 2022 at 2:45
4

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 :).

1
  • I've been looking through loads of templates recently and it is very common. You're right that I understand what the loop is doing, I just don't understand why people would choose to do it in this particular way. Why use single_post_title rather than the_title()?
    – olliew
    Commented Apr 23, 2016 at 13:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.