开发者

is_front_page() not working wordpress

I'm trying to load a script from "func开发者_JAVA技巧tions.php" just when i'm in the front page.

I set a static page called "home" in the reading options.

The home page loads the "front-page.php" template correctly but the conditional script loading doesn't work.

This is what I have in my "functions.php" file:

wp_register_script('nivoslider', get_bloginfo('template_url').'/js/libs/nivoslider.js', false, false, true);

if (is_front_page()) {   
   wp_enqueue_script('nivoslider'); 
}

Why isn't this loading as expected? What's happening here?


$isfrontpage = false;   
if( is_front_page() || is_home() ) {    

    $isfrontpage = true;    

} else {

    $current = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    $site = get_site_url();
    $site = str_replace('http://', '', $site);
    $site = str_replace('https://', '', $site);
    $site = str_replace('www', '', $site);

    if( $site == $current || $site . '/' == $current ){
        $isfrontpage = true
    }

}


From the Codex -

It returns TRUE when the main blog page is being displayed and the Settings->Reading->Front page displays is set to "Your latest posts", or when is set to "A static page" and the "Front Page" value is the current Page being displayed.

Just setting it to use your front page template does not make it a front page. It is the front page when either of the conditions above are true. Therefore both must be false for this page you created.

Also, is this just included inline with functions.php or are you actually hooking into the head?


is_front_page(); will not work in functions.php as expected since the functions.php initializes before its able to detect your "front_page.php"


Here's the right way to enqueue scripts:

<?php

add_action( 'wp_enqueue_scripts', 'my_custom_enqueue_scripts', 5 );

function my_custom_enqueue_scripts(){
    wp_register_script('nivoslider', get_bloginfo('template_url').'/js/libs/nivoslider.js', false, false, true);

    if (is_front_page()) {   
       wp_enqueue_script('nivoslider'); 
    }
}

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜