php - 在 wordpress 的 CSS 中更改选定页面的最有效方法是什么?

标签 php html css wordpress

我想给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);

这应该给你一个想法

关于php - 在 wordpress 的 CSS 中更改选定页面的最有效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25378077/

相关文章:

php - 在 WordPress 插件中插入 .sql 文件中的数据

html - Div 标签之间的交替背景颜色

javascript - 共享类似于 addthis/sharethis 的替代方案

html - 我可以在一个 CSS 选择器中同时使用百分比 (100%) 和像素 (-30px)

html - 带有 HTML 和 CSS 的代码元素破坏了我在 Shopware 中的购物世界

css - 如何跨浏览器垂直对齐文本

php - 在网站托管期间出现此错误

php - 从 CodeIgniter Active Record 类调用存储过程

php - 在 MySQL 的 php 中显示最后一个用户 ID

javascript - 删除或隐藏 svg 元素