0

I have a page that I designated as my front page using the reading portion of settings in the dashboard. When I place a script inside of an if statement using is_front_page in functions.php, it only runs on that designated front-page, and not the others (indicating the page has been designated correctly).

However, I have an if statement in my header.php file that also uses is_front_page, and that seems to be triggering on all my pages (including the ones that I have not designated as the front page).

What is going on? Can you not run is_front_page from within header.php and have that code only apply to the front-page header? Am I missing something?

This is the code that I have placed in my header.php file that is returning true on all pages

<?php echo ( is_front_page ) ? 'frontPageLogo' : NULL; ?>

(I believe it is returning true on all pages because it always echoes 'frontPageLogo')

  • 1
    There seem to be either some round brackets missing or in the wrong position in the code line you posted. – JHoffmann Nov 29 '16 at 18:24
  • 2
    the ternary statement should have had is_front_page() (with parentheses) – czerspalace Nov 29 '16 at 18:28
  • jHoffmann & czerspalace you were both right, thank you, those got my ternary statement working. – CoderScissorhands Nov 29 '16 at 18:34
5

Yes it should work in the header file just like normal. Try using a standard IF statement instead of shorthand:

if( is_front_page() ){
    echo 'Front page!';
}

You must have it configured in the settings to use:

A static page (select below) instead of Your latest posts

If you want is_front_page() to only return TRUE when viewing the page you select from the dropdown for Front Page (which it sounds like you do)

You can then use is_home() if a static page is set for the front page of the site, this function will return true only on the page you set as the “Posts page”.

https://developer.wordpress.org/reference/functions/is_home/

Whether is_home() or is_front_page() return true or false depends on the values of certain option values:

  • get_option( 'show_on_front' ): returns either 'posts' or 'page'
  • get_option( 'page_on_front' ): returns the ID of the static page assigned to the front page
  • get_option( 'page_for_posts' ): returns the ID of the static page assigned to the blog posts index (posts page)

When using these query conditionals:

  • If 'posts' == get_option( 'show_on_front' ):
    • On the site front page:
      • is_front_page() will return true
      • is_home() will return true
    • If assigned, WordPress ignores the pages assigned to display the site front page or the blog posts index
  • If 'page' == get_option( 'show_on_front' ):
    • On the page assigned to display the site front page:
      • is_front_page() will return true
      • is_home() will return false
    • On the page assigned to display the blog posts index:
      • is_front_page() will return false
      • is_home() will return true
  • when you say I must "have it configured" do you mean the front-page or the other pages. As I said is_front_page works exclusively on the designated front page when I use functions.php file. It is only the header.php that is acting strangely. – CoderScissorhands Nov 29 '16 at 18:19
  • Ok, the code you posted only fired on front-page when I inserted in on header-php so there is something wrong with my ternary statement. Nothing seems to be wrong with the static settings, they are all as they should be. Thank you! – CoderScissorhands Nov 29 '16 at 18:22
0

Just for the record, your original statement would have worked if you had called a function by including parens instead of using what looked like a non-existent constant to the PHP interpreter. The constant is_front_page was evaluated as the string "is_front_page" and since "is_front_page" is not == false, it evaluated as true and "frontPageLogo" was always echoed. Get it?

So instead of:

<?php echo ( is_front_page ) ?  'frontPageLogo' : NULL; ?>

You wanted:

   <?php echo ( is_front_page() ) ?  'frontPageLogo' : NULL; ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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