Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Maja B.
Maja B.
12,984 Points

Flexslider only on Front Page

Hi, I'm using Flexslider and it works fine.

The only thing I cannot do is to use it only on front-page.php

Here is the code that works (but on all pages, not only on front-page)

add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);

function enqueue_child_theme_styles() {
    wp_enqueue_style( 'pho', get_template_directory_uri().'/style.css' );
    wp_enqueue_style( 'pho-child', get_stylesheet_uri(), array('pho')  );
    wp_enqueue_style( 'grid', get_stylesheet_directory_uri() . '/css/grid.css');
    wp_enqueue_style( 'flexslider', get_stylesheet_directory_uri() . '/css/flexslider.css');
}

function theme_js() {
    wp_enqueue_script( 'theme_js', get_stylesheet_directory_uri() . '/js/theme.js', array( 'jquery' ), '', true );
    wp_enqueue_script( 'flexslider', get_stylesheet_directory_uri() . '/js/jquery.flexslider-min.js', array('jquery'), '', false );
}

add_action('wp_enqueue_scripts', 'theme_js');

Here is the code that I thing should work nly on front-page. But it does not.

add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
function enqueue_child_theme_styles() {
    wp_enqueue_style( 'pho', get_template_directory_uri().'/style.css' );
    wp_enqueue_style( 'pho-child', get_stylesheet_uri(), array('pho')  );
    wp_enqueue_style( 'grid', get_stylesheet_directory_uri() . '/css/grid.css');

    wp_register_style( 'flexslider', get_stylesheet_directory_uri() . '/css/flexslider.css' );
    if( is_page( 'front-page' ); ) {
        wp_enqueue_style( 'flexslider' );
    }
}

function theme_js() {
    wp_enqueue_script( 'theme_js', get_stylesheet_directory_uri() . '/js/theme.js', array( 'jquery' ), '', true );

    wp_register_script( 'flexslider', get_stylesheet_directory_uri() . '/js/jquery.flexslider-min.js', array('jquery'), '', false );
    if( is_page( 'front-page' ) ) {
        wp_enqueue_script( 'flexslider' );
    }
}

add_action('wp_enqueue_scripts', 'theme_js');

If anyone knows, please tell me what is wrong with the second code.

2 Answers

Agustin Grube
Agustin Grube
39,278 Points

Or you can try to test for the front page with this:

if ( is_front_page() ) {
    wp_enqueue_style( 'flexslider' );
}
Maja B.
Maja B.
12,984 Points

This works. Thanks!!

Agustin Grube
Agustin Grube
39,278 Points

Try deleting the ; after the Front Page:

From this...

if( is_page( 'front-page' ); ) {
        wp_enqueue_style( 'flexslider' );

to this...

if( is_page( 'front-page' ) ) {
        wp_enqueue_style( 'flexslider' );