I am trying to add action hook in functions.php only if the page is home.php
but the following does not work
if ( is_page_template("home.php") ) {
// do stuff
}
To make sure that file being used is home.php
I do this
global $template; echo $template;
Use
is_home
instead, asis_page_template
will not work forhome.php
as its technically not a page template in the traditional sense.Revised:
The revised answer is actually what you really need, it will check if you are on the home page, in addition to getting the template file being included and check to see if that file is in fact your
home.php
file and if so, you can go about your logic…I’ve had this problem. I wanted to include Jssor Slider scripts only when loading the home page. I was using a home.php too, and I’ve solved it this way:
Settings > Reading > A static page > Front page: Home
)is_page('home')
to work. (‘home’ is my empty new home page slug). That is, when I was trying to conditionally add the action, I wasn’t able to verify if my home page was loading,is_page('home')
always returnedfalse
. It worked when I changed the logic for always adding the action but conditionally enqueuing the scripts.The code that worked out on functions.php:
This way my Jssor scripts are loaded only when acessing the home page, not when acessing other pages or posts.
The function you are looking for is
is_front_page()