【问题标题】:remove css if sidebar is not present on wordpress如果 wordpress 上不存在侧边栏,则删除 css
【发布时间】:2015-10-13 16:48:29
【问题描述】:

这对于背景色应用来说有点奇怪,我有这个主要结构

<section class="main-row">
    <div class="container">
        <div class="row">
            <div id="primary" class="col-sm-12 content-area">
</div>
<div id="secondary" class="col-sm-4 sidebar widget-area">
<div>
</div>
</div>
</section>

在主行我应用了一个 CSS 编辑媒体查询来调整宽度:

.main-row {
  padding: 70px 0;
/* background: linear-gradient( to right, #ffffff 0%, #ffffff 62%, #f0f0f0 38%, #f0f0f0 100% ); */
}

现在,如果有侧边栏,这可以工作,但是如果整页我想删除背景以使其仅变为纯白色,是否可以在 php 中使用?我只想让侧边栏有一个纯色背景,延伸全高和全宽,但容器和行设置为将内容调整为仅 1170px,但如果侧边栏是,我希望侧边栏是全高和右侧颜色如果侧边栏在左侧,则在右侧和左侧。

【问题讨论】:

标签: html css wordpress


【解决方案1】:

如果模板使用侧边栏,请使用 body_class 过滤器将类添加到正文(将 my-sidebar-template.php 替换为您的侧边栏模板名称):

add_filter('body_class', 'sidebar_template_class');
function sidebar_template_class($classes){
  if(get_page_template_slug(get_the_ID()) == 'my-sidebar-template.php')
      $classes[] = 'has-sidebar';

  return $classes;
}

使用 CSS 移除作为 body.has-sidebar 后代的任何 .main-rows 的背景:

CSS

.main-row {
    background: linear-gradient( to right, #ffffff 0%, #ffffff 62%, #f0f0f0 38%, #f0f0f0 100% );
}
.has-sidebar .main-row{
    background: none;
}

【讨论】:

  • 优雅解决方案!我喜欢它。
  • 尝试将此添加到我的functions.php,现在我的wordpress是白色的,不幸的是我的ftp客户端给我一个错误530 FTP Access Denied,由于可疑的FTP活动(121.58. 247.2)
  • 抱歉,我留下了一个错字。查看更新。 (以分号结束 $classes[] = 行)。
  • 不幸的是尝试了代码它仍然显示我的sidear名称只是sidear.php调用只是
  • 您需要将my-sidebar-template.php 更改为模板的路径。 IE。 template-parts/sidebar.php
【解决方案2】:

终于想出了一个更实用的解决方案:

.sidebar .inner {
 border-left: none;
margin-left: 59px;
padding-left: 30px;
background-color: #f0f0f0;
margin-right: -3000px;
padding-right: 3000px;
padding-bottom: 3000px;
margin-bottom: -3000px;
margin-top: -130px;
padding-top: 50px;

}

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签