Archived topic
Register Sidebar
13 replies · Started by Tesco on August 7, 2022
I'm trying to use custom sidebars on my theme without success...
register_sidebar( array(
'name' => __( 'Category', 'mytheme' ),
'id' => 'category-widget-area',
'description' => __( 'The category widget area', 'mytheme' ),
'before_widget' => '<section class="%1$s %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3 class="myclass">',
'after_title' => '</h3>',
) );
Is this possible on the theme?
Hi Tesco,
It should be possible. This would be out of our scope of support though. You can refer to this for assistance: https://developer.wordpress.org/themes/functionality/sidebars/
There’s also a plugin for this you may use: https://wordpress.org/plugins/content-aware-sidebars/
Hi Fernando,
How can I use my custom sidebars where I need?
I was watching this: https://docs.generatepress.com/article/sidebar-layout/ ...
Did you create your custom sidebar from scratch?
Why not just create a Block Element - Sidebar?: https://docs.generatepress.com/article/block-element-sidebar/
I prefer this way (hard coded). How can I use them where I need?
Hi there,
you can use the dynamic_sidebar function to display your sidebar:
<?php dynamic_sidebar( 'your_custom_sidebar_name' ); ?>
More info here:
https://developer.wordpress.org/reference/functions/dynamic_sidebar/
And i suggest reading the article Fernando provided:
https://developer.wordpress.org/themes/functionality/sidebars/
Hi David,
How can I apply this to the theme archive.php without messing with core files?
That depends on how you intend to display them.
Simplest method would be to hook them inside the Themes sidebar. For example:
List of them:
generate_before_right_sidebar_content
generate_after_right_sidebar_content
generate_before_left_sidebar_content
generate_after_left_sidebar_content
You can use a GP Hook Element to select any of those hooks and make your callback:
<?php dynamic_sidebar( 'your_custom_sidebar_name' ); ?>
or a snippet eg.
add_action( 'generate_before_right_sidebar_content', function() {
// set your IF display condition
if ( is_single() ) {
dynamic_sidebar( 'your_custom_sidebar_name' );
}
});
May I have your pro opinion on this?
Create a shortcode to include a php file called sidebar-condition.php and inside of this file create an IF display condition with the custom content.
Shortcodes are the go to method if you want to add the function inside the post editor.
If you're adding the code for your sidebar using a function or GP Hook then i would not bother with the shortcode.
Just let me know if I'm right...
First I register my sidebar like this:
register_sidebar(
array(
'name' => 'SidebarOnCats',
'id' => 'SideBar-On-Cats',
'before_widget' => '<aside id="%1$s" class="widget inner-padding %2$s">',
'after_widget' => '</aside>',
'before_title' => apply_filters( 'generate_start_widget_title', '<h2 class="widget-title">' ),
'after_title' => apply_filters( 'generate_end_widget_title', '</h2>' ),
)
);
Then I call this sidebar on functions.php like this:
add_action( 'generate_before_right_sidebar_content', function() {
// set your IF display condition
if ( is_archive() ) {
dynamic_sidebar( 'SidebarOnCats' );
}
});
Then, inside the widget I write whatever I want...
Is this right?
When you register a sidebar it has to be hooked into the widgets_intit - see here in the provided docs for an example:
https://developer.wordpress.org/themes/functionality/sidebars/#examples
Thanks!
You're welcome