WP中的自定义“侧边栏”

时间:2016-06-25 23:42:24

标签: php css wordpress woocommerce

我在wordpress网站上与WooCommerce一起运行Avada主题。

问题是我正在尝试在页面顶部获取搜索功能,但仅限于响应模式(在移动设备上)。我已经能够为woocommerce中的侧边栏添加第三个选项。这显示在配置中(在哪里选择应该进入的小部件)。但是我不能让它显示出来。

我知道你会得到这样的原始侧边栏(直接从原始archive-product.php复制):

<?php
        do_action( 'woocommerce_sidebar' );
?> 

所以我认为应该可以使用以下行调用我们自己的侧栏:

<?php
        do_action( 'woo_sidebar-3' );
?>

但这不起作用。 由于必须只能在响应设备上显示,我们使用自定义css融合选项以下列方式隐藏其他侧边栏:

@media only screen and (max-width: 720px) {
    #sidebar {
        display: none;
    }
}

这当然太简单了,因为它会阻止所有侧边栏。但这是我们可以轻易改变的事情。到目前为止,我们创建的第3个侧边栏不会显示在网站的响应式或桌面版本上。而其他人确实在桌面上显示,但没有在响应中显示(根据需要)。

修改 - :

所以我尝试使用以下代码:

<?php if ( !function_exists('dynamic_sidebar') ||
           !dynamic_sidebar('woo_sidebar_3') ) { ?>
     <p>Oops... this displays if the topbar is not set up</p>
<?php } ?>

它显示了代码中给出的错误,因此它尝试在我们创建时拉出侧边栏。但它没有显示出来。是因为以下可能无法正常工作吗? :

'woo_sidebar_3' => array(
    'label'           => esc_html__( 'Global WooCommerce Product Sidebar 3', 'Avada' ),
    'description'     => esc_html__( 'Custom sidebar 3', 'Avada' ),
    'id'              => 'woo_sidebar_3',
    'default'         => 'None',
    'type'            => 'select',
    'choices'         => $sidebar_options,
    'active_callback' => array( 'Avada_Options_Conditionals', 'is_woo' ),

哪一个是从上面的那个复制的,叫做woo_sidebar_2?

编辑2 - :

Here is a JSfiddle包含完整的sidebars.php代码,以及dynamic_sidebar代码(作为HTML)。当你运行te脚本时,我指出的错误也是可见的。

1 个答案:

答案 0 :(得分:1)

我对Avada主题不太熟悉,无法了解其中某些选项的含义,即使Avada必须使用它,您的代码块也不会显示register_sidebar()来电。

为简化起见,您可以使用registering a widget area

的标准WordPress代码

在您的主题(理想情况下是儿童主题)中添加:

function so_38033924_widgets_init() {

    register_sidebar( array(
        'name'          => __( 'Global WooCommerce Product Sidebar 3', 'your-theme' ),
        'id'            => 'woo_sidebar_3',
        'description'   => __( 'Custom sidebar 3', 'your-theme' ),
        'before_widget' => '<div class="widget">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>',
    ) );

}
add_action( 'widgets_init', 'so_38033924_widgets_init' );

您需要确保before_widgetbefore_title参数类型与父主题匹配,以便您的小部件获得相同的样式。

然后你的模板代码对我来说是正确的:

<?php if ( !function_exists('dynamic_sidebar') ||
           !dynamic_sidebar('woo_sidebar_3') ) { ?>
     <p>Oops... this displays if the topbar is not set up</p>
<?php } ?>
相关问题