2

我正在尝试在Wordpress中输出一个侧边栏,具体取决于它是否具有小部件(处于活动状态),并相应地在我的 Shop 页面上显示它们。

我对 PHP 很不熟悉,到目前为止,我已经编写了以下脚本来尝试使其工作,但它似乎没有发生。

商店.php

<?php
/**
 * The template for displaying the Shop page.
 */

get_header(); ?>
    <div id="primary" class="site-content">
        <div id="content" role="main">
            <?php shop_content(); ?>
        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar('shop'); ?>
<?php get_footer(); ?>

侧边栏-shop.php

<?php
/**
 * The sidebar containing the Shop page widget areas.
 *
 * If no active widgets are in the sidebars (Left, Right and theShop), 
 * the sidebars should be hidden completely.
 */
?>

    <?php 
    // If the left, shop and right sidebars are inactive
if ( ! is_active_sidebar( 'right-sidebar' ) && ! is_active_sidebar( 'shop-sidebar' ) ) {
    return;
}

    // If there are active widgets in the Shop Sidebar
    if ( is_active_sidebar( 'shop-sidebar' ) ) { ?>
        <div id="secondary" class="widget-area" role="complementary">
            <?php dynamic_sidebar( 'shop-sidebar' ); ?>
        </div><!-- #secondary -->
    <?php 
    }

    // If there are active widgets in the Right Sidebar
    elseif ( is_active_sidebar( 'right-sidebar' ) ) { ?>
        <div id="secondary" class="widget-area" role="complementary">
            <?php dynamic_sidebar( 'right-sidebar' ); ?>
        </div><!-- #secondary -->
    <?php   
    }
    ?>

边栏.php

<?php
/**
 * The sidebar containing the main widget area.
 *
 * If no active widgets in sidebar, let's hide it completely.
 *
 */
?>

    <?php if ( is_active_sidebar( 'right-sidebar' ) ) : ?>
        <div id="secondary" class="widget-area" role="complementary">
            <?php dynamic_sidebar( 'right-sidebar' ); ?>
        </div><!-- #secondary -->
    <?php endif; ?>

我将如何修改上面的脚本以输出以下内容:

  • 如果右侧边栏(right-sidebar)有小部件,显示右侧边栏
  • 如果 Shop Sidebar (shop-sidebar) 有小部件,则显示 Shop Sidebar
  • 如果右侧边栏和商店边栏都有小部件,则显示商店边栏
  • 如果右侧边栏或商店边栏都没有任何小部件,则不要显示任何内容

谢谢你。

4

3 回答 3

0
// if both are active shoe shop
if (is_active_sidebar('shop') && is_active_sidebar('sidebar-1')) {
      get_sidebar('shop');
}
//if shop is active, show shop
elseif (is_active_sidebar('shop')) {
   get_sidebar('shop');
}
// if sidebar 1 is active show it.
elseif (is_active_sidebar('sidebar-1')) {
    get_sidebar('sidebar-1');
}
于 2013-03-18T06:44:21.730 回答
0

代码应该很简单,因为您已经定义了商店侧边栏优于右侧边栏的良好优先级。如果您不需要显示侧边栏,则无需担心。

<?php

// If shop sidebar is active, it takes precedence
if ( is_active_sidebar( 'shop' )){
   get_sidebar( 'shop' ); 
}
elseif (is_active_sidebar( 'sidebar-1' )){
   get_sidebar( 'sidebar-1' );
   //This is "shop side bar is inactive and right sidebar is active"
}

// That's all; just two condition blocks!
?>

谢谢。

于 2013-03-18T06:53:11.553 回答
0

它正在被functions.php文件修改。

于 2013-03-25T15:50:38.117 回答