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' );
}
Related
I've created a custom widget area in the plugin I'm creating.
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' );
dynamic_sidebar( 'home_right_1' );
And to enable showing the widgets stored in this widget I have inserted this code inside the footer.php of the theme I'm currently using for testing.
<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-
area" role="complementary">
<?php dynamic_sidebar( 'home_right_1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
It works, when I add widgets to my widget area, they are shown in footer of the website. But I want to make the plugin works without changing the footer.php manually. How can I check which theme is activated and then find that theme, the footer.php file and add that code exacltly where I want (to be shown below any other footer created with the theme when is installed. Also, I would like to remove the footer that was generated before.
i am working on a simple WordPress blog and i am trying to display a widget that appears in every page in the website .. i have followed the dynamic sidebar widget approach, but the problem ,
here is the Function.php file
function wpb_widgets_init() {
register_sidebar( array(
'name' => 'Custom Header Widget',
'id' => 'custom-header-widget',
'before_widget' => '<div class="chw-widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="chw-title">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'wpb_widgets_init' );
and my header.php file
<?php if ( is_active_sidebar( 'custom-header-widget' ) ) : ?>
<div id="header-widget-area" class="chw-widget-area widget-area" role="complementary">
<?php dynamic_sidebar( 'custom-header-widget' ); ?>
</div>
<?php endif; ?>
and this works fine only in any single post.
but in the home page/or any template file nothing shows !!!
Any ideas ?
You can try this structure and make sure your sidebar active
I have figured it out
i hade to create a template file for my new sideBar called: sidebar-.php
and use the get_sidebar(id) method to call it
Voilet it works in all templates ...
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'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;
}
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