0

In the following snippet, when I add the wp_enqueue_scripts call, the page freezes. What am I doing wrong?

function my_enqueue_stuff() {
  if ( is_front_page() ) {
    wp_enqueue_style('style', get_template_directory_uri() . '/css/homepage.css', array(), null, 'all');
  } else if ( is_page( 'corsi' ) ) {
    wp_enqueue_style('style', get_template_directory_uri() . '/css/corsi.css', array(), null, 'all');
  } else if ( is_page( 'dove-siamo' ) ) {
    wp_enqueue_style('style', get_template_directory_uri() . '/css/dove-siamo.css', array(), null, 'all');
    wp_enqueue_scripts('js', get_template_directory_uri() . '/lib/map/gmap.js')
  } else if ( is_page( 'contatti' ) ) {
    wp_enqueue_style('style', get_template_directory_uri() . '/css/contatti.css', array(), null, 'all');
  } else if ( is_page( 'insegnanti' ) ) {
    wp_enqueue_style('style', get_template_directory_uri() . '/css/insegnanti.css', array(), null, 'all');
  } else if ( strpos($_SERVER['REQUEST_URI'], 'galleria') ) {
    wp_enqueue_style('style', get_template_directory_uri() . '/css/galleria.css', array(), null, 'all');
  } else {
    /** do nothing */
  }
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_stuff' );
  • 4
    The function is called wp_enqueue_script, not scripts. I'm talking about the dove-siamo case. – tfrommen Apr 16 '13 at 16:55
0

I'm not sure you can use is_page() in the function.php (citation needed!). Instead of enqueuing the the style sheets, you could echo them in the header.php.

So, in the header.php you'd want...

<?php
if ( is_front_page() ) {
    echo '<link rel="stylesheet" type="text/css" href="' . get_template_directory_uri() . '/css/homepage.css">';
} else if ( is_page( 'corsi' ) ) {
    echo '<link rel="stylesheet" type="text/css" href="' . get_template_directory_uri() . '/css/corsi.css">';
  • Of course you can. In the functions.php file you define a function that is hooked into the wp_enqueue_scripts action, that again is triggered on a frontend page. Meaning, when the function is executed, you sure can use is_page. – tfrommen Apr 16 '13 at 17:53
  • You know you're missing a semicolon after 'wp_enqueue_scripts('js', get_template_directory_uri() . '/lib/map/gmap.js')**;**, right? – David Kryzaniak Apr 16 '13 at 18:04
0

This should do what you want:

function my_enqueue_scripts() {
    if (is_front_page())
        wp_enqueue_style('style', get_template_directory_uri().'/css/homepage.css', array(), null, 'all');
    elseif (strpos($_SERVER['REQUEST_URI'], 'galleria'))
        wp_enqueue_style('style', get_template_directory_uri().'/css/galleria.css', array(), null, 'all');
    else {
        $slug = get_post($GLOBALS['wp_query']->post->ID)->post_name;
        switch ($slug) {
            case 'corsi':
                wp_enqueue_style('style', get_template_directory_uri().'/css/corsi.css', array(), null, 'all');
                break;
            case 'dove-siamo':
                wp_enqueue_style('style', get_template_directory_uri().'/css/dove-siamo.css', array(), null, 'all');
                wp_enqueue_script('js', get_template_directory_uri().'/lib/map/gmap.js');
                break;
            case 'contatti':
                wp_enqueue_style('style', get_template_directory_uri().'/css/contatti.css', array(), null, 'all');
                break;
            case 'insegnanti':
                wp_enqueue_style('style', get_template_directory_uri().'/css/insegnanti.css', array(), null, 'all');
                break;
        }
    }
}
add_action('wp_enqueue_scripts', 'my_enqueue_scripts');

Notes:

  1. You could think about using the $slug inside your wp_enqueue_style if you have for each page a separate CSS file named {$slug}.css.
  2. Why do you do this (using multiple stylesheets) anyway? Just use one instead and handle the different cases by using the body class page-id-{ID} or add your own body classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.