Is_front_page inside header.php is always returning true
-
16-04-2021 - |
Question
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')
Solution
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 theID
of the static page assigned to the front pageget_option( 'page_for_posts' )
: returns theID
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 returntrue
is_home()
will returntrue
- If assigned, WordPress ignores the pages assigned to display the site front page or the blog posts index
- On the site front page:
- If
'page' == get_option( 'show_on_front' )
:- On the page assigned to display the site front page:
is_front_page()
will returntrue
is_home()
will returnfalse
- On the page assigned to display the blog posts index:
is_front_page()
will returnfalse
is_home()
will returntrue
- On the page assigned to display the site front page:
OTHER TIPS
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; ?>