我想在wordpress中添加一组页面的背景图片。
我想为另一组页面添加不同的背景图片
目前,我使用PageID将其应用于每个单独的页面,如下面的代码所示。因为有1000多页。是否有更简单的方法来应用于每一组页面?
.page-id-264 #header .logo a,
.page-id-272 #header .logo a {
background-image: url("http://www.richcoward.com/newcges/wp-content/uploads/2014/08/NWU-No-Frame.png") !important;
background-size: 100% !important;
background-repeat: no-repeat !important;
height: 86px !important;
width: 416px !important;
}
谢谢你的帮助
这里有一个简单的解决方案。
在你的主题文件夹中创建一个自定义css文件(比如mystyle.css)。添加
#header .logo a {
background-image: url("http://www.richcoward.com/newcges/wp-content/uploads/2014/08/NWU-No-Frame.png") !important;
background-size: 100% !important;
background-repeat: no-repeat !important;
height: 86px !important;
width: 416px !important;
}
放入你的mystyle.css。现在,打开functions.php并有条件地对样式表进行排队。使用is_page()
或is_page_template()
条件检查。我相信你说的是页面组,所以我可以想象你为特定的组使用特定的页面模板,所以我倾向于is_page_template
function enqueue_custom_style() {
if(is_page_template('page-whatever.php')) {
wp_enqueue_style('my-style', get_stylesheet_directory_uri() . '/mystyle.css' );
}
}
add_action('wp_enqueue_scripts', 'enqueue_custom_style', 999);
这应该给你一个想法
您可以在functions.php文件中使用过滤器向所有相关页面添加一个类。您仍然需要快速地记下每个页面ID,但我猜在PHP中这样做将使您的CSS更小(因此加载更快):
// Add specific CSS class by filter
add_filter( 'body_class', 'my_class_names' );
function my_class_names( $classes ) {
// array of Page IDs
$pageids = array(264, 272);
// if
if ( is_page( $pageids ) ) {
// add 'class-name' to the $classes array
$classes[] = 'class-name';
// return the $classes array
return $classes;
}
}
或者,为每个背景创建页面模板