I'm creating a custom WordPress template which has 4 "sidebars" one for the header, a separate sidebar for the left and right columns, and then another sidebar for the footer, all that's fine and working properly. However, with my template, the header sidebar can only hold one widget at a time, so I was wondering if there was a way to add multiple widgets to the sidebar, but manipulate the code somehow to only display a random (or even in some specific order) widget at a time, which will change on each new page reload.
Code I used to make the sidebar:
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'Header Sidebar Widgets',
'id' => 'sidebar-widgets-header',
'description' => 'The Header Can Only Support One (1) Widget.',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
And then the code I used to add the sidebar to my template:
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar-widgets-header') ) :
endif;
Here is an image of the sidebar within my admin currently:
(source: illstudios.com)
Quote One and Featured Videos are custom widgets I created. So is there a way to only choose one random widget from the selection and only display one widget at a time?
There's a filter that returns all widgets in all sidebars. We can shuffle and trim one of the sidebars with it:
if( !is_admin() )
{
add_filter( 'sidebars_widgets', 'sidebar_so_23691473' );
}
function sidebar_so_23691473( $sidebars_widgets )
{
shuffle( $sidebars_widgets['sidebar-widgets-header'] );
$only_one = array_slice( $sidebars_widgets['sidebar-widgets-header'] , -1 );
$sidebars_widgets['sidebar-widgets-header'] = $only_one;
return $sidebars_widgets;
}
Related
I want to display default Woo-commerce product filter in my custom plugin development. I have used following short-code for display products.
echo do_shortcode("[products]");
It's working and product also showing but i also want to display default product filter in Woo-commerce , I used following short-code but default product filter is not showing.
[woocommerce_product_filter_products]
[woocommerce_product_filter]
Is there another way to display default product filter in plugin development or another pages?
Register a Sidebar:
if ( function_exists('shortcodeWidget') )
register_sidebar(array(
'name' => 'Shortode widget for filter',
'id' => 'shortcode-widget',
'before_widget' => '<div class = "filterWidget">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
)
);
After register the shortcode
function shortcodeSidebar(){
ob_start();
get_sidebar('shortcode-widget');
$sidebar= ob_get_contents();
ob_end_clean();
return $sidebar;
}
add_shortcode('filterWoocommerce', 'shortcodeSidebar');
And then add the filter in the custom widget.
So, I'm making my own theme from scratch for Wordpress with the Woocommerce plugin. I can seem to find how to add a sidebar widget to my Shop page... There is no area for it in thw Widgets menu and I can't seem to find how to create it and edit it...
Can someone help me understant how to create it and which files/code do I need to create in my child theme?
You need to create a widget area in your functions.php with this function :
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name' => 'Sidebar',
'before_widget' => '<div class = "widget">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
)
);
And for display in your theme :
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Sidebar") ) : ?>
Try this on header.php for example, you can add sidebar where u want
if ( is_active_sidebar( 'Sidebar' ) ) {
dynamic_sidebar( 'Sidebar' );
}
I am running a Avada theme together with WooCommerce on a wordpress site.
The issue is that I'm trying to get a search function on the top of the page, but only in responsive mode (on mobile devices). I've been able to add a third option for a sidebar in woocommerce. this shows up in the config (where to choose which widgets should go in). But I can NOT get it to show.
I know that you get the original sidebars like this (copied directly from original archive-product.php):
<?php
do_action( 'woocommerce_sidebar' );
?>
So I figured it should be possible to call for our own sidebar with the following line :
<?php
do_action( 'woo_sidebar-3' );
?>
This does not work though.
Since this has to be able to be shown only on responsive devices we used a custom css option of fusion to hide the other sidebars the following way :
#media only screen and (max-width: 720px) {
#sidebar {
display: none;
}
}
Which of course is too simple since it would block ALL sidebars. But that's something we could easily change. So far, the 3rd sidebar that we created does not show on responsive nor on desktop versions of the site. while the others do show on the desktop but not on the responsive (as desired).
EDIT-:
So i tried this with the following code :
<?php if ( !function_exists('dynamic_sidebar') ||
!dynamic_sidebar('woo_sidebar_3') ) { ?>
<p>Oops... this displays if the topbar is not set up</p>
<?php } ?>
It shows the error as given in the code, so it tries to pull the sidebar as we created. But it doesn't show. Is it because the following might not work correctly? :
'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' ),
Which was copied from the one right above that, which is called woo_sidebar_2?
EDIT 2 -:
Here is a JSfiddle with the complete sidebars.php code in it, and the dynamic_sidebar code (as HTML). the error I was pointing at is also visible when you run te script.
I'm not familiar enough with the Avada theme to know what some of those options mean and your block of code doesn't show the register_sidebar() call even though Avada must be using it.
To make things simpler, you could use standard WordPress code for registering a widget area
In your theme (ideally a child theme) add:
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' );
You would need to make sure the before_widget, before_title, type of parameters match the parent theme so that your widget will get the same styling.
And then your template code looks right to me:
<?php if ( !function_exists('dynamic_sidebar') ||
!dynamic_sidebar('woo_sidebar_3') ) { ?>
<p>Oops... this displays if the topbar is not set up</p>
<?php } ?>
Hello Guys Im trying to put a search box in my menubar and I was able to do it BUT how can I use the woocommerce search product instead??
heres my current Code in the function.php:
add_filter( 'wp_nav_menu_items','add_search_box', 10, 2 );
function add_search_box( $items, $args ) {
$items .= '<li>' . get_search_form( false ) . '</li>';
return $items;
}
I want to put Woocommerce_product_search in my MENUBAR anybody can help me.
SOrry for the ENglish im really not good in it.
Register a Sidebar in your Functions.php and then add the sidebar into your container where you want the search box to appear. Then add the Woocommerce Search Widget to this sidebar inside the Wordpress Admin interface.
Add this to Functions.php:
register_sidebar(array(
'id' => 'search-widget',
'name' => __('Search Widget'),
'description' => __('Drag search widget to this container'),
'before_widget' => '<article id="%1$s" class="widget %2$s">',
'after_widget' => '</article>',
'before_title' => '<h6>',
'after_title' => '</h6>'
));
}
Add this to your Header.php or whichever file contains the code where the search box needs to go:
<?php dynamic_sidebar("search-widget"); ?>
I am new to wordpress. I use the code below to display secondary widgets. This code shows all recent posts in the sidebar but I want a secondary widget for a specific category.
<?php dynamic_sidebar( 'secondary-widget-area' ); ?>
You have to register/declare your widgetized area before you can use it. This happens in the functions.php, so that your theme is aware of the widget area.
<?php
/**
* Register our sidebars and widgetized areas.
*
*/
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Home right sidebar',
'id' => 'home_right_1',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
?>
If you're just trying to add a secondary widget to an existing widgetized area, then no coding is needed. Just go to Appearance>Widgets and drag and drop HTML blocks or other custom widgets to the appropriate area.
More on Using Widgets
More on Widgetizing