我正在尝试在 Wordpress 中输出侧边栏,具体取决于它是否有小部件(处于活动状态),并相应地在我的商店页面上显示它们。
我对PHP很不熟悉,到目前为止已经编写了以下脚本来尝试使其工作,但似乎并没有发生。
shop.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
}
?>
的sidebar.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; ?>
如何修改上面的脚本以输出以下内容:
谢谢。
答案 0 :(得分: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');
}
答案 1 :(得分: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!
?>
感谢。
答案 2 :(得分:0)
functions.php
文件正在修改它。