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.

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' );
share|improve this question
4  
The function is called wp_enqueue_script, not scripts. I'm talking about the dove-siamo case. –  t f Apr 16 '13 at 16:55

2 Answers 2

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">';
share|improve this answer
    
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. –  t f 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

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.
share|improve this answer

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.