在下面的代码段中,当我添加wp_enqueue_scripts call
, 页面冻结。我做错了什么?
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\' );
SO网友:tfrommen
这应该满足您的需要:
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:
您可以考虑使用$slug
在您的wp_enqueue_style
如果每个页面都有一个单独的CSS文件,名为{$slug}.css
.无论如何,为什么要这样做(使用多个样式表)?只需使用一个,并使用body类处理不同的情况page-id-{ID}
或者添加自己的身体类别