Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

How to target blog page specifically, using conditional tag when 'is_home()' also returns true for static front page

Hi,

I have set up my site with a static front page and a 'news' page to display blog posts.

On my static front page, I have assigned a custom loop to display the newest sticky post alongside the static content.

I am writing a conditional statement so that the front page sticky post will display in full, but all the posts on the blog page will display as excerpts.

However, since my static front page contains a version of the loop, it also seems to be returning true when I am using the conditional tag is_home().

This is making it impossible for me to target the front page and the blog page separately using conditional tags.

Here is the code I've written: <?php

        if ( is_single() || is_front_page()  ) {
            the_content();
        }

        elseif ( is_search() || is_archive() || is_home()  ) {

            the_excerpt();
        }

        else  {
            the_content( );
        }
        ?>

Is there another way I could conditionally target the blog page using a tag other than 'is_home()'?

Or am I approaching this entirely the wrong way to begin with?

Any pointers would be very much appreciated.

5 Answers

kevin jordan
kevin jordan
11,353 Points

Hey Fiona -

You can use the WP function 'get_the_ID()' to return an integer value for each of the pages. You can set your logic from there. I have also used 'get_the_title()' to accomplish similar tasks.

Hope this helps !! kj

Hi Kevin, Thanks for replying. Not sure I am understanding you correctly however.

My page ID for my blog posts is page 14.

So I am re-writing my code like this, replacing 'is_home()' with 'is_page( 14 )':

<?php

    if ( is_single() || is_front_page()  ) {
        the_content();
    }

    elseif ( is_search() || is_archive() || is_page( 14 )  ) {

        the_excerpt();
    }

    else  {
        the_content( );
    }
    ?>

This isn't targeting the blog page though. Am I using the wrong syntax or the wrong logic somewhere?

kevin jordan
kevin jordan
11,353 Points

I was thinking more like this

<?php
 $postid = get_the_ID(); 
if ( is_single() || is_front_page()  ) {
        the_content();
    }

    elseif ( is_search() || is_archive() || $postid == 14  ) {

        the_excerpt();
    }

    else  {
        the_content( );
    }


?> 

There are several ways to clean up your logic, resulting in quicker execution. Basically since you know the ID of what you're looking for, you can test for that ID then decide which road to take - either the_excerpt() or the_content();, Hope this helps !!

kj

The logic makes total sense, but still isn't letting me target the blog page for some reason.

Thanks for your help though, is very much appreciated :-)

Think am just approaching this wrong to begin with somehow. None of the syntax seems to be quite working the way I am expecting it to, so I must be missing something conceptually here.

Found a solution of sorts, in case anyone else should ever encounter this problem.

Since I wasn't able to figure out how to target the posts page using a conditional tag or statement, I created a home.php template file instead and created a modified version of the index.php file contents within that, which now only displays the post excerpts, and only applies to the posts page itself.

This enables me to treat the output of the static front page and posts page loops separately.

Clumsy and not particularly ideal, but does the job. Hey-ho.