Site logo

Archived topic

Register Sidebar

13 replies · Started by Tesco on August 7, 2022

Viewing posts 1–14 of 14

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?

I prefer this way (hard coded). How can I use them where I need?

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:

https://github.com/tomusborne/generatepress/blob/8bff7e067bb4df92ce1711fddb843585d4d18e71/sidebar.php#L20

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?

Thanks!

You're welcome

This archived topic is closed to new replies.