Take the 2-minute tour ×
WordPress Development Stack Exchange 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
add comment

3 Answers 3

First issue:

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.

I will assume that by home page, you actually mean site front page. In that case, you don't really care about is_home() or home.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:

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.

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:

if ( is_home() && wp_script_is( 'jquery', 'done' ) )
  1. is_home(): You need to use is_front_page(), if you intend to target the site front page
  2. wp_script_is( 'jquery', 'done' ): this seems to be unnecessary; you merely need to register your script with jquery as a dependency

Third issue:

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 ?

This is, essentially, _doing_it_wrong(). You should have no need to override user settings. Just pick the right conditionals:

  1. To target the site front page, use is_front_page()
  2. To ensure that the site front page is displaying a static page, use 'page' == get_option( 'show_on_front' ).
  3. If the front page is set to display a static page, then the user will have created and assigned a static page to use. To determine the ID for that page, use get_option( 'page_on_front' ).

Putting it all together

  1. Add your script to a custom.js file in the Theme directory somewhere.
  2. Add a callback hooked into wp_enqueue_scripts():

    add_action( 'wp_enqueue_scripts', 'wpse58196_enqueue_custom_script' );`
    
  3. Inside the callback, enqueue your custom script based on the correct conditionals:

    function wpse58196_enqueue_custom_script() {
        // If we're on the site front page, and the front page displays a static page
        if ( is_front_page() && 'page' == get_option( 'show_on_front' ) ) {
            // Enqueue custom script, with jQuery as a dependency, and output in the footer
            wp_enqueue_script(
                // handle
                'custom-carousel',
                // path
                get_template_directory() . '/custom.js',
                // dependency array
                array( 'jquery' ),
                // version
                '1.0',
                // in footer?
                true
            );
        }
    }
    
share|improve this answer
add comment

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
add comment

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
add comment

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.