Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 ?

share|improve this question

2 Answers

What you need is:

if ( is_front_page() || is_page_template( 'home.php' ) {     
     //javscript stuff...

}
share|improve this answer
Technically home.php is not a page template, it's the main page for the blog. is_page_template() will only return true if a page has a custom template that you select from the sidebar. – Rezen Jul 12 '12 at 16:13

Your issue is more likely related to wp_script_is().

Try this instead

function aa_home_scripts() {
    if(is_home()){
        wp_enqueue_script(
            'app-home',
            get_template_directory_uri() . '/js/app.home.js',
            array('jquery'), // declare jQuery as dependancy
           '1',
           true // flag to add to footer
       );
    }
}
add_action('wp_enqueue_scripts', 'aa_home_scripts');
share|improve this answer
No , is_home returns false – Jashwant Jul 11 '12 at 20:16
I just tested and it worked fine for me. home.php is the base for the blog. So if you change the Reading page to a specific page it will load that template. If you want the front page / homepage of your website and you set a front page then use the is_front_page() – Rezen Jul 11 '12 at 21:05
is_front_page() is false too. Moreover your method does not use inline js. – Jashwant Jul 11 '12 at 21:08
Try is_blog() function mentioned here. – Rezen Jul 11 '12 at 21:13

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.