【问题标题】:Woocommerce conditional tags for custom sidebar is twentyseventeen theme自定义侧边栏的 Woocommerce 条件标签是二十七主题
【发布时间】:2018-10-06 20:07:55
【问题描述】:

我正在使用二十七主题和 Woocommerce,并希望为我的 woo 商务页面设置一个自定义侧边栏,而不是网站其他地方使用的默认主题侧边栏 (sidebar-1)。

所以在我的子主题的functions.php中,我注册了我的自定义侧边栏(sidebar-6),用小部件填充它,并尝试在我的子主题的sidebar.php中使用woo commerce条件标签来调用它

我是条件标签的新手,尽管在各种在线网站上遵循了建议,但到目前为止还无法让它发挥作用。如果有人能指出我正确的方向,我将不胜感激。

原来的主题sidebar.php是这样的

if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>

<aside id="secondary" class="widget-area" role="complementary" 
aria-label="<?php esc_attr_e( 'Blog Sidebar', 'twentyseventeen' ); 
?>">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->

我尝试了各种代码组合来尝试完成这项工作,但到目前为止都失败了。这是我目前拥有的,它导致任何页面上都没有显示侧边栏。

if ( is_woocommerce() ) : {
return;
}
?>

<aside id="secondary" class="widget-area" role="complementary" 
aria-label="<?php esc_attr_e( 'Shop Sidebar', 'twentyseventeen' );    
?>">
<?php dynamic_sidebar( 'sidebar-6' ); ?>
</aside><!-- #secondary -->


<?php else:
return;
}

?>
<aside id="secondary" class="widget-area" role="complementary" 
aria-label="<?php esc_attr_e( 'Blog Sidebar','twentyseventeen');         
?>">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->
<?php endif ?>

非常感谢您对此的任何建议!

【问题讨论】:

    标签: woocommerce sidebar twentyseventeen


    【解决方案1】:

    你在以下情况中犯了一个错误:

    if ( is_woocommerce() ) : {
    return;
      }
    

    基本上你是说如果它是 Woocommerce 页面返回并且什么都不做。

    这是正确的:

    if ( is_woocommerce() && is_active_sidebar( 'sidebar-6' ) ) :?>
    <aside id="secondary" class="widget-area" role="complementary" 
    aria-label="
        <?php esc_attr_e( 'Shop Sidebar', 'twentyseventeen' ); ?>">
        <?php dynamic_sidebar( 'sidebar-2' ); ?>
    </aside><!-- #secondary -->
        <?php
        elseif ( is_active_sidebar( 'sidebar-1' ) ) :
            ?>
    <aside id="secondary" class="widget-area" role="complementary" 
    aria-label="<?php esc_attr_e( 'Blog Sidebar', 'twentyseventeen' ); ?>">
            <?php dynamic_sidebar( 'sidebar-1' ); ?>
    </aside><!-- #secondary -->
    <?php endif; ?>
    

    【讨论】:

    • @SusanNeil 不客气,如果您将答案标记为正确答案并向上箭头,我将不胜感激:)
    • 你好,我希望我没有说得太早......侧边栏没有正常工作。所以它被拉到商店页面,但后来表现得很奇怪。没有显示我放入的小部件,而是引入了不同的东西。然后当我再次尝试时,拉入正确的小部件,但后来又显示了其他东西。您对这里可能发生的事情有任何想法吗?谢谢
    • @SusanNeil 所以内容在页面加载之后或期间发生变化?你能把链接发在这里检查吗?
    • 好像解决了!我从全新安装开始,然后从头开始重新构建网站......这次离开了 Gutenberg 和 Woo Commerce 块构建器。不知道这些是否导致了问题,或者是否是其他东西悄悄进入了。但现在一切正常,所以手指交叉仍然是这种情况。再次感谢@kasolo。
    【解决方案2】:

    我以前用过这个插件,它会做你需要它做的事情。它还可以让您更轻松地添加更复杂的场景。

    https://en-gb.wordpress.org/plugins/custom-sidebars/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 2020-05-27
      • 2018-11-12
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      相关资源
      最近更新 更多