Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to target my landing page with is_front_page or is_home in my page.php file but it's not working. I have set the settings > reading to a static page.

However, if I create a page-home.php everything works fine (without the tags of course). But, if I use the is_front_page or is_home on the index file they will work (on my blog page).

Am I just misunderstanding their function and if so, how can I target the landing page dynamically? Thanks.

<?php get_header(); ?>

<?php if ( is_front_page() || is_home() ) : ?>
    <h1>Why not???</h1>
<?php endif; ?>

<div>
    <article>
        <?php get_template_part( 'loop', 'page' ); ?>
    </article>

    <aside>
        <?php get_sidebar(); ?>
    </aside>
</div>

<?php comments_template(); ?>

<?php get_footer(); ?>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Ok, if you take a look at implementation of is_front_page, you will see the following:

/**
 * Is the query for the front page of the site?
 *
 * This is for what is displayed at your site's main URL.
 *
 * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
 *
 * If you set a static page for the front page of your site, this function will return
 * true when viewing that page.
 *
 * Otherwise the same as @see WP_Query::is_home()
 *
 * @since 3.1.0
 * @uses is_home()
 * @uses get_option()
 *
 * @return bool True, if front of site.
 */
function is_front_page() {
    // most likely case
    if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
        return true;
    elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
        return true;
    else
        return false;
}

So I think the answer of your question is in this sentence If you set a static page for the front page of your site, this function will return true when viewing that page. It means that to make your page.php working as you want, you need to go to the "Settings" -> "Reading" page and set "Front page displays" option.

share|improve this answer
    
Thanks Eugene, I have the reading settings set to static page (just clarified that above). But the problem still exists. –  marcup Mar 23 '12 at 22:11
    
Is the above snippet from the wp core? I don't assume that I have to implement it, or? –  marcup Mar 23 '12 at 22:13
    
Yes it is from the WP core, it is WP_Query class. –  Eugene Manuilov Mar 23 '12 at 22:14
    
Add var_dump(get_option( 'show_on_front')); and var_dump(get_option( 'page_on_front' )); to see what they return. –  Eugene Manuilov Mar 23 '12 at 22:15
    
Where exactly should I add them? –  marcup Mar 23 '12 at 22:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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