嗨,我使用这段代码为单个post类型调用不同的样式表,但问题是它不会调用样式表。
它在header.php里,我也试着把它放在single.php里
<?php
if ( ! is_home() ) {
if ( is_single() == 'pretty-little-liars' ) {
echo '<link rel="stylesheet" href="http://www.tv-cafe.com/wp-content/themes/tvcafe/posttypecss/style-pll.css" type="text/css" media="screen" />';
}
}
?>有人能告诉我问题出在哪里吗?
发布于 2013-01-05 20:07:14
is_single()返回TRUE或FALSE,而不是字符串。此外,还可以通过将post段塞放入函数调用中来测试具有is_single()函数的特定post:
if ( is_single( 'your-post-slug' ) )
{
# do something
}如果要测试正确的post类型,请使用:
if ( is_singular() and 'your-post-type' === get_post_type() )或者只是:
if ( is_singular( 'your-post-type' ) )对于您的具体问题,您应该将该脚本包装成一个回调,并连接到wp_enqueue_scripts中。在functions.php中:
function wpse78368_enqueue_custom_stylesheet() {
if ( is_singular( 'pretty-little-liars' ) ) {
wp_enqueue_style(
'style-pll',
get_template_directory_uri() . '/posttypecss/style-pll.css'
);
}
}
add_action( 'wp_enqueue_scripts', 'wpse78368_enqueue_custom_stylesheet' );注意:使用wp_enqueue_scripts是因为wp_enqueue_styles并不以do_action()的形式存在。
https://wordpress.stackexchange.com/questions/78368
复制相似问题