I want to include a custom inline js on home page only.
I havent set home page from reading section.
I am using home.php file.
function print_my_inline_script() {
if (is_home() && wp_script_is( 'jquery', 'done' ) ) {
?>
<script type="text/javascript">
jQuery(document).ready(function($){
$('.carousel').carousel();
$('.item').hover(function(e){
$(this).find('.summary').stop().fadeTo('fast',1);
},function(){
$(this).find('.summary').stop().fadeOut('fast');
});
});
</script>
<?php
}
}
add_action( 'wp_footer', 'print_my_inline_script' );
This doesnt work. is_front_page()
doesnt work either.
I have already done <?php wp_reset_query(); ?>
after loop.
I have one more question. I know home.page overrides index.php
and works as home page.
But I dont want my users to get confused when they change options in reading
section.
I have found this,
update_option( 'page_on_front', $post->ID );
update_option( 'show_on_front', 'page' );
but it needs an ID and I dont have any page, so I dont have any id.
So, I need a way to set check if user is in home.php
(home page) and since using home.php overrides the reading section options, any workarounds for that ?
First issue:
I will assume that by home page, you actually mean site front page. In that case, you don’t really care about
is_home()
orhome.php
, as these apply to the blog posts index, regardless of whether you are on the site front page or some other page.See also:
Again, the confusion here regards home page as site front page versus blog posts index. Refer to the Template Hierarchy for site front page display for further explanation.
User reading settings will have no impact, if you choose the correct conditional with which to enqueue your script.
Second issue:
This conditional in your callback:
is_home()
: You need to useis_front_page()
, if you intend to target the site front pagewp_script_is( 'jquery', 'done' )
: this seems to be unnecessary; you merely need to register your script withjquery
as a dependencyThird issue:
This is, essentially,
_doing_it_wrong()
. You should have no need to override user settings. Just pick the right conditionals:is_front_page()
'page' == get_option( 'show_on_front' )
.get_option( 'page_on_front' )
.Putting it all together
custom.js
file in the Theme directory somewhere.Add a callback hooked into
wp_enqueue_scripts()
:Inside the callback, enqueue your custom script based on the correct conditionals:
Your issue is more likely related to
wp_script_is()
.Try this instead
What you need is: