Styling for WordPress sidebar not looking right - dynamic_sidebar() - wordpress

Custom sidebars has been a breeze so far except with it comes unto the blog 'Category'.
Notice how it looks perfectly here on the 'Page' version - http://70.87.35.71/~life/?page_id=87
but the same sidebar has different elements the category version - http://70.87.35.71/~life/?cat=1
Why does the sidebar look so crappy when under the category section and how do I make it look like the 'Page' version.
My sidebar code is below
<div id="sidebar" class="clearfix">
<?php
if ( !in_category('1') ) {
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar') )
{}
}
else {
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Blog') )
{}
}
?>
</div> <!-- end sidebar -->

There is no need to check if function_exists. It has existed since WordPress version 2.1.
Your display problem is due the sidebar blog sidebar not being registered properly.
add_action( 'widgets_init', 'add_blog_sidebar' );
function add_blog_sidebar() {
register_sidebar(array(
'name' => 'Blog Sidebar',
'id' => 'blog',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
));
}
The before widget and before title arguments are needed to allow for styling.
Calling your dynamic sidebar:
if ( is_category('1') dynamic_sidebar( 'blog' );
else dynamic_sidebar( 'sidebar');
To learn everything there is to know about sidebars check out Justin Tadlock's sidebar tutorial.

Try changing it to:
<?php
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Blog') )
{}
?>
remove the if else statement.

Related

WordPress: Is it possible to add a widget to footer/sidebar on widget activation?

I have managed to follow fuxia's solution on adding a default widget to the footer sidebar but the widget doesnt seem to be "activated" (I have to remove and manually add it back to the footer sidebar for the widget to work, making the pre-set widget code pointless). I found that the widget added by the aforementioned solution and the same widget pulled from the list of available widgets are different. The picture below shows different href values for the widget items in the footer list of widgets. (Grey boxes are just the widget name)
I'm not sure if this is the reason behind the issue but is there a way to add the widget to the footer sidebar AND have it work/"activated"?
EDIT: current implementation
add_action( 'widgets_init', function(){
register_widget( 'MyWidget' );
$sidebars = array ('a' => 'sidebar-1' );
foreach ( $sidebars as $sidebar ) {
register_sidebar(array (
'name' => 'Footer',
'id' => $sidebar,
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>'
));
}
$active_widgets = get_option( 'sidebars_widgets' );
if ( ! empty ( $active_widgets[ $sidebars['a'] ] )){
return; }
$counter = 1;
$active_widgets[$sidebars['a']][0] = 'my_widget-'.$counter;
update_option('sidebars_widgets',$active_widgets);
dynamic_sidebar('sidebar-1');
}
Can you check with the following code?
functions. php
add_action( 'widgets_init', 'custom_sidebars' );
/*Custom widgets*/
function aavishkaar_custom_sidebars(){
register_sidebar(
array (
'name' => __( 'Footerwidget', 'Aavishkaar' ),
'id' => 'footer-widget1',
'description' => __( 'Footer widget', 'Aavishkaar' ),
'before_widget' => '<div class="col-md-2 col-3 footer-pd_0 widget_ul">',
'after_widget' => "</div>",
)
);
}
Then add menu to this and call it by <?php dynamic_sidebar('footer-widget1');?>

How to create a widgetized sidebar for a theme with no sidebar?

Please could someone show me how to do this? My theme (Fable) does not have a built-in sidebar and I need one for my homepage.
Many thanks!
Create a child theme.
Create a file sidebar.php.
In your sidebar.php-file you have to use at least the following function: dynamic_sidebar()
Furthermore in your functions.php file of your child Theme you must use the following functions: register_sidebar()
Include the sidebar in your child theme (most probably in the overwritten index.php-file)
Style the sidebar to integrate it with your Theme properly.
For further documentation see the WordPress Codex:
https://codex.wordpress.org/Child_Themes
https://codex.wordpress.org/Function_Reference/dynamic_sidebar
https://codex.wordpress.org/Function_Reference/register_sidebar
Put this in your functions.php file:
<?php
// Declare sidebar widget zone
register_sidebars( 1,
array(
'name' => 'My Widget Area',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>'
)
);
?>
Then put this in the place in your theme where you want the widget area to appear:
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('My Widget Area')) : else : ?><?php endif; ?>

Add widget to post-new.php i the dashboard?

How can i add a custom widget to wp-admin/post-new.php?
Some themes use it, and i wan't to be able to do the same thing in my theme :)
Thanks
/Richard
You can create yourself a new side bar in functions.php, copy and paste one of the current ones, eg:
register_sidebar( array (
'name' => 'Primary Widget Area',
'id' => 'primary_widget_area',
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => "</li>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
Change any references to primary to a name of your choice maybe post-new
Then add this side bar to wp-admin/edit-form-advanced.php. Search for 'side-info-column'. Just before that div closes ad in the call to the new side bar:
<?php if ( is_sidebar_active('secondary_widget_area') ) : ?>
<div id="secondary" class="widget-area">
<ul class="xoxo">
<?php dynamic_sidebar('secondary_widget_area'); ?>
</ul>
</div><!-- #secondary .widget-area -->
<?php endif; ?>
Again, you can copy one of the current ones found in sidebar.php.
You can then go to dashboard > Appearance > Widgets and you should see your new sidebar there.
Hope that helps. I'll be happy to help more if needs be.
Found what I was looking for. Its called meta boxes and there are lots of tutorials how to implement them. Just Google it ;)

How to display my wordpress widget programmatically?

I need to display my custom made widget in WP page based custom page template programmatically.
This is the scenario:
I created page based on custom page template called "product" now I need in its sidebar display my custom made widget. I need this side bar to be totally different from other pages this is why I want to display it programmatically.
I tried to use the_widget() function but I think it works only with built in widgets and also I don't know how to pass parameters registered with register_sidebar function to it, because it seems it doesn't use them by default.
Actually I used this : the_widget('WP_Widget_Search') to test it and widget was there but it ignored theme settings, I mean settings in function.php :
register_sidebar( array(
'name' => 'Sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div></div>',
'before_title' => '<h3>',
'after_title' => '</h3><div class="padder">'
) );
So as I said I don't know if it works with custom made widgets (created in plugins) because I don't know how to pass widget name there and params. How to do that?
Dynamically create a dynamic instance of the widget using the_widget(), eg:
<div id="xxx" class="widget-area" role="complementary">
<div id="sidebar-dyn">
<?php the_widget( 'WP_Widget_Recent_Posts' ); ?>
</div>
</div>
Why the need to include custom widget programatically. I mean you can simply create another sidebar that is shown only on your custom page template "product"
Further as you need to do it for your custom page, you don't even need to check for any condition. Simply put this code in your "prdouct" page template file anywhere ( where you would like to display your sidebar".
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Product Sidebar')) ;?>
And simply drag your widget to this sidebar from admin panel.
If the widget has a shortcode, call it using this:
<?php echo do_shortcode( $content ) ?>
you're question isn't exactly clear to me, but what i am gathering is that
1. you need to register your widget and
2. you need for it to ONLY show on the product page.
to register, place this in functions.php (you may already have the first line in your functions file):
<?php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name'=> 'Product Sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div></div>',
'before_title' => '<h3>',
'after_title' => '</h3><div class="padder">'
));
?>
this would be the code to include in sidebar.php to display the above only on the page called "Product".
<?php if (is_page('Product')) ;?>
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Product Sidebar')) ;?>
<?php endif; ?>

wordpress widget prob

I am using widgets in my wordpress theme and I need to remove the:
<div class="textwidget"></div>
that appears around the widget's content. I am using this in my function.php file:
if (function_exists('register_sidebar'))
register_sidebar(array('name'=>'Church Address',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<p class="bold">',
'after_title' => '</p>',
));
but I need to remove the div from around the text widget's body. Is there some way to do this?
I also have another question. When I put let's say, a tag in the before_widget and a in the after_widget shouldn't it put a paragraph tag around the entire widget? Instead, it put them both before the entire widget.
On your page.php the code should be something like:
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Church Address') ) : ?>
<address>
<p class="bold">
The Church Address goes here if the sidebar isn't working or not loading
</p>
</address>
<?php endif; ?>
then enable your sidebar widget: admin->appearance->widgets
the code which is inside your functions.php file should be fine. although if using the p tag, I would encompass this in an address tag for better read ability, also its supported by all the major browsers..
if (function_exists('register_sidebar'))
register_sidebar(array('name'=>'Church Address',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<address class="bold">',
'after_title' => '</address>',
));

Resources