【问题标题】:Wordpress dynamic_sidebar displays nothingWordpress dynamic_sidebar 什么也不显示
【发布时间】:2017-02-20 20:11:57
【问题描述】:

我已经在 wordpress 支持上发布了这个,但是这里的答案要快得多...

这是一个“复制粘贴”:

我想要 home.php 模板上的额外小部件区域。我正在使用 Emil Uzelac 的响应式主题。我不知道为什么我的额外“侧边栏”不起作用。

dynamic_sidebar('front-side-sidebar');

该函数返回 true,但它什么也不显示。我可以在“小部件”中看到我的额外小部件区域,并且我确实在那里添加了一个小部件。在我的functions.php中我有这个:

function front_side_sidebar_init()
{
    register_sidebar(
        array(
            'id' => 'front-side-sidebar',
            'name'=>'Front Side sidebar',
            'description' => __('Front Side sidebar', 'responsive'),
            'before_widget' => '<li>',
            'after_widget' => '</li>',
            'before_title' => '<h3 class="widget-title">',
            'after_title' => '</h3>'
        )
    );
}
add_action( 'widgets_init', 'front_side_sidebar_init' );

【问题讨论】:

  • 您的代码看起来不错,尝试在条件中添加is_active_sidebar( 'front-side-sidebar' );,看看您的侧边栏是否处于活动状态

标签: wordpress dynamic sidebar


【解决方案1】:

这就是我的做法,我不知道主题是否有在主题选项中创建新侧边栏的功能,所以这就是我回答的原因。

在 Functions.php 中添加这个

register_sidebar(array(
  'name' => __( 'Front Side sidebar' ),
  'id' => 'front-side-sidebar',
  'description' => __( '' ),
  'before_widget' => '<div class="widget">',
  'after_widget'  => '</div>',
  'before_title' => '<h3>',
  'after_title' => '</h3>'
));

如果你设置了静态首页,那么wordpress会使用front-page.php。如果你没有这个,它将使用 page.php,我的英语不好,但在这里查看更多。 HERE!

在您的模板中(front-page.php 或 page.php home.php 您正在使用什么) 用这个替换你的侧边栏区域()

<?php get_sidebar('front-page'); ?>

此代码将查找文件 sidebar-front-page.php 并在该文件中添加此代码。在模板目录下添加sidebar-front-page.php

<?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('front-side-sidebar')) : ?>(you can also put code here if you dont have anything in the this sidebar, it will show this function as standard if you dont have any thing on this sidebar.<?php endif; ?>

长话短说,此代码将检查 id 为“front-side-sidebar”的侧边栏并显示您在侧边栏中设置的小部件。

是的,这应该可以! :)

【讨论】:

  • 我试图避免再有一个文件..但这有效,所以我会保持这种方式...谢谢
【解决方案2】:

你解决了这里的问题吗?这听起来可能很愚蠢,但请确保代码被您的 PHP 标签包围&lt;?php ?&gt;

【讨论】:

  • 听起来确实很傻...我说函数返回 true 所以你可以假设我知道 PHP。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 2020-10-21
相关资源
最近更新 更多