I'm trying to output a sidebar in Wordpress depending on whether it has widgets (is active) or not and to display them on my Shop page accordingly.
I'm pretty unfamiliar with PHP, and so far have written up the following scripts to try and make this work, but it doesn't seem to be happening.
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(); ?>
sidebar-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; ?>
How would I modify the script above to output the following:
if the Right Sidebar (right-sidebar) has widgets, display the Right Sidebar
if the Shop Sidebar (shop-sidebar) has widgets, display the Shop Sidebar
if the Right Sidebar and Shop Sidebar both have widgets, display the Shop Sidebar
if neither the Right Sidebar or the Shop Sidebar have any widgets, don't display anything
Thank you.
// 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');
}
Code should be simple, because you have defined good precedence of Shop sidebar over right sidebar. You don't need to worry if you don't need to display the sidebars.
<?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!
?>
Thanks.
It is being modified by the functions.php file.
Related
Hey Guys Extremely sorry for the silly question because I am newbie to wordpress. I just wanted to show user testimonials.
For that I Installed Easy Testimonials Plugin.
Then I creates a new page called Testimonials. In that I give the below shortcode
[testimonials theme="default_style" paginate="all" count="5" orderby="date" order="ASC" show_title="0" use_excerpt="0" show_thumbs="1" show_date="1" show_other="1" hide_view_more="1" output_schema_markup="1" show_rating="stars"]
Upto now all are working fine, But in my site its showing that shortcode that I have given in the testimonial page.
My admin panel
This is my page.php
<?php
/**
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that other
* 'pages' on your WordPress site will use a different template.
*
* #package OceanWP WordPress theme
*/
get_header(); ?>
<?php do_action( 'ocean_before_content_wrap' ); ?>
<div id="content-wrap" class="container clr">
<?php do_action( 'ocean_before_primary' ); ?>
<div id="primary" class="content-area clr">
<?php do_action( 'ocean_before_content' ); ?>
<div id="content" class="site-content clr">
<?php do_action( 'ocean_before_content_inner' ); ?>
<?php
// Elementor `single` location
if ( ! function_exists( 'elementor_theme_do_location' ) || ! elementor_theme_do_location( 'single' ) ) {
// Start loop
while ( have_posts() ) : the_post();
get_template_part( 'partials/page/layout' );
endwhile;
} ?>
<?php do_action( 'ocean_after_content_inner' ); ?>
</div><!-- #content -->
<?php do_action( 'ocean_after_content' ); ?>
</div><!-- #primary -->
<?php do_action( 'ocean_after_primary' ); ?>
<?php do_action( 'ocean_display_sidebar' ); ?>
</div><!-- #content-wrap -->
<?php do_action( 'ocean_after_content_wrap' ); ?>
<?php get_footer(); ?>
I suggest you to go to admin panel Testimonials->All Testimonials select edit testimonial and check description content there
in your page.php put below code anywhere and check it working or not
<?php echo do_shortcode('[testimonials theme="default_style" paginate="all" count="5" orderby="date" order="ASC" show_title="0" use_excerpt="0" show_thumbs="1" show_date="1" show_other="1" hide_view_more="1" output_schema_markup="1" show_rating="stars"]'); ?>
For some reason my footer disappeared for my site http://juliannaspizza.com.
I checked the template file for the home page and get_footer is being called as shown below. Any idea why my footer is still not showing up?
<?php
/**
* Template Name: Full Width Page
*
* #package Fortune
*/
get_header();
get_template_part( 'parts/single', 'page-header' ); ?>
<div class="container full-width-page">
<div class="row">
<div id="primary" class="content-area col-lg-12">
<main id="main" class="site-main" role="main">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'content', 'page' );
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile; ?>
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .row -->
</div><!-- .container -->
<?php
get_footer();
This is what my footer used to look like (https://certifagift.blog/):
Your widgets are gone. From the markup, it looks like your footer (which does appear) is expecting to have widgets, but those are not appearing. Go to Appearance -> Widgets in your dashboard and add your widgets back in, and you should be good.
Based on a particular value(appID) I want to hide one of the widget ID in my wordpress blog.
my widget Id : custom_html-2 ( I got this value from enabling accessibility mode)
( from this url I got widgetID : /wp-admin/widgets.php?widgets-access=on&_wpnonce=0b123456fb2&editwidget=custom_html-2&sidebar=right-sidebar&key=0 )
Here I want to hide custom_html-2 when the value of appID is in.abcd.app .
I just echoed appID , I am getting the value in.abcd.app , but the widget is not getting hidden.
I opened the Sidebar (sidebar.php) in the word press and did modification as bellow. Didnt workout - I am new to wordpress and PHP
<!-- Sidebar -->
<div class="col-lg-4 col-md-4 col-sm-4">
<aside class="sidebar">
<?php if ( is_active_sidebar( 'right-sidebar' ) ) : ?>
<div class="sidebar-widget">
<?php dynamic_sidebar( 'right-sidebar' ); ?>
<?php
<?php $widgets = get_option('widget'.sidebar-widget); ?>
$appID = $_SERVER['HTTP_X_REQUESTED_WITH']
if ($appID = 'in.abcd.app')
{
<?php unset($widgets['custom_html-2']); ?>
?>
</div>
<?php endif; ?>
</aside>
</div>
<!-- /end sidebar -->
You'll want to add something like the following to your functions.php
add_filter( 'sidebars_widgets', 'unset_sidebar_widget' );
function unset_sidebar_widget( $sidebars_widgets ) {
$appID = $_SERVER['HTTP_X_REQUESTED_WITH'];
if ($appID = 'in.abcd.app')
{
unset($sidebars_widgets['custom_html-2']);
}
return $sidebars_widgets;
}
I suppose dynamic_sidebar( 'right-sidebar' ); renders the sidebar and the widgets, so if you unset a widget after the widget was rendered, it's not going to change anything.
Try to put the dynamic_sidebar( 'right-sidebar' ); after the unset part.
Im using the UnderScore theme and below is the main page.php template file. What im aiming for is if there is active widgets in the sidebar use template with sidebar but if no active widgets or content use template that has the main content as 960px with no sidebar. Im also using ACF (advance custom fields) is the sidebar so would need to check that aswell. Gratefull for any help. I thought maybe using "is_active_sidebar" but unsure how to implerment it properly as template parts are already being check.
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
Also below the sidebar.php
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>
<aside id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
<?php the_field('sidebar_info'); ?>
</aside><!-- #secondary -->
Maybe I'm not fully understanding you question, but you can use a simple if-statement for that. See pseudo code below.
if ( active widgets in the sidebar ) {
// use this formatting
} else {
// use that formatting
}
Stared a Wordpress site using Underscores theme (_s)
I have got one sidebar working but want to make a second one to be on the same page. (containing different widgets)
I have added the new sidebar to the functions.php and it appears in the Wordpress login screen and i can drop widgets into it. However, I can't get it to show on the actual webpage. (the first sidebar is working fine)
Anyone know how to do this or know a tutorial...
Thanks
You'll need to edit the "sidebar.php" file in your theme folder. It should normally be:
<div id="secondary" class="widget-area" role="complementary">
<?php do_action( 'before_sidebar' ); ?>
<?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>
<?php endif; // end sidebar-1 widget area ?>
</div><!-- #secondary -->
You will need to add another if statement that looks out for the other sidebar. Be sure to reference it by whatever you used in the functions.php file.
If you want it to appear below the current sidebar, just be sure to add it before closing the 'div' so it will still be in the same column. I used 'sidebar-lower':
<div id="secondary" class="widget-area" role="complementary">
<?php do_action( 'before_sidebar' ); ?>
<?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>
<?php endif; // end sidebar-1 widget area ?>
<?php if ( ! dynamic_sidebar( 'sidebar-lower' ) ) : ?>
<?php endif; // end sidebar-lower widget area ?>
</div><!-- #secondary -->