【问题标题】:WordPress Roots theme get rid of sidebar from all PagesWordPress Roots 主题摆脱了所有页面的侧边栏
【发布时间】:2013-10-22 07:18:29
【问题描述】:

我的目标是摆脱所有页面上的侧边栏,但我的网站安装了 Roots,因此 page.php 模板只有以下代码:

<?php get_template_part('templates/page', 'header'); ?>
<?php get_template_part('templates/content', 'page'); ?>

第一个问题,这是什么意思?这些模板“部分”在哪里?其次,如何去掉部分或全部页面的侧边栏?

编辑:在 templates/content-page.php 中添加代码:

<?php while (have_posts()) : the_post(); ?>
  <?php the_content(); ?>
  <?php wp_link_pages(array('before' => '<nav class="pagination">', 'after' => '</nav>')); ?>
<?php endwhile; ?>

我有更多好东西。这是来自 lib/sidebar.php。看起来 Roots 有一些方法来决定单个元素,基于它们上的标志或其他东西,是否显示它们。有 PHP 知识的人可以解释一下这可能会做什么吗?

<?php
/**
 * Determines whether or not to display the sidebar based on an array of conditional tags or page templates.
 *
 * If any of the is_* conditional tags or is_page_template(template_file) checks return true, the sidebar will NOT be displayed.
 *
 * @param array list of conditional tags (http://codex.wordpress.org/Conditional_Tags)
 * @param array list of page templates. These will be checked via is_page_template()
 *
 * @return boolean True will display the sidebar, False will not
 *
 */
class Roots_Sidebar {
  private $conditionals;
  private $templates;

  public $display = true;

  function __construct($conditionals = array(), $templates = array()) {
    $this->conditionals = $conditionals;
    $this->templates    = $templates;

    $conditionals = array_map(array($this, 'check_conditional_tag'), $this->conditionals);
    $templates    = array_map(array($this, 'check_page_template'), $this->templates);

    if (in_array(true, $conditionals) || in_array(true, $templates)) {
      $this->display = false;
    }
  }

  private function check_conditional_tag($conditional_tag) {
    if (is_array($conditional_tag)) {
      return $conditional_tag[0]($conditional_tag[1]);
    } else {
      return $conditional_tag();
    }
  }

  private function check_page_template($page_template) {
    return is_page_template($page_template);
  }
}

【问题讨论】:

  • 在模板/页面的文件夹中查找,找到header.php,虽然这不是你想要的,但在page.php(第二个template_part())里面会有一个get_sidebar()您需要删除的功能 =]
  • 很难理解你的英语。没有文件夹模板/页面。在 templates/ 中有一个 header.php,但它没有提到侧边栏。我不知道你最后一句话是什么意思。有一个 templates/content-page.php 文件,但没有提及侧边栏。我将编辑我的原始问题以添加其中的代码。
  • 不一定在循环中,但更有可能在循环之外。 (否则,如果有 5 个帖子,将打印 5 个以上的侧边栏)。也找dynamic_siderbar()
  • templates/content-page.php 中的循环之外没有任何内容。我发布了它的全部内容。 dynamic_sidebar() 在哪里?
  • 在根目录下查看 lib 文件夹,有 config.php。该函数右侧模板名称中有一个函数roots_display_sidebar,它不显示侧边栏,就像它已经有template-custom.php一样。自定义模板不显示侧边栏。

标签: php css wordpress


【解决方案1】:

在根主题主页上有这方面的指南:

http://roots.io/the-roots-sidebar/

上面提到的是 Ali Exhalter,但基本上你需要找到 lib/config.php 文件并将适当的函数名称添加到数组列表中。在这种情况下,如果您希望从所有页面中删除,is_page 将起作用。

config.php 如下所示:

function roots_display_sidebar() {
  $sidebar_config = new Roots_Sidebar(
  array(
  'is_404',
  'is_front_page',
  'is_page'
  ),
  /**
   * Page template checks (via is_page_template())
   * Any of these page templates that return true won't show the sidebar
   */
  array(
    'template-custom.php'
  )
);

也可以指定隐藏侧边栏的某些模板。

【讨论】:

    【解决方案2】:

    在没有安装主题本身的情况下,它看起来有主题选项,可以让您使用标准 WP 管理区域禁用侧边栏。这将是一个推荐的选项,因为它使您可以在以后重新激活侧边栏。但是,如果您想永久删除侧边栏,请尝试注释掉 base.php 的第 21-25 行;这应该删除对根侧​​边栏功能的调用。

    【讨论】:

      【解决方案3】:

      据我所知,没有通过 WordPress 管理员从根目录中删除侧边栏的选项。在您的子主题的functions.php 中抛出以下 sn-p 以完全删除侧边栏:

      // functions.php
      add_filter('roots_display_sidebar', 'roots_sidebar_on_special_page');
      function roots_sidebar_on_special_page($sidebar) {
        return false;
      }
      

      您还可以根据页面数据有条件地禁用侧边栏,some additional info on that here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-07
        • 1970-01-01
        • 2014-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-04
        相关资源
        最近更新 更多