0

I'm developing a new theme for WP and I want to add different sorts of widgets and different classes, because I want these widgets to look different (for example footer widgets should have another styling, and now I can only target them with the "widget class", one class for all).

function create_widget($name, $id, $description) {

  register_sidebar(array(

'name' => __( $name ),

    'id' => $id,
    'description' => __( $description ),
    'before_widget' => '<div class="widget">',
    'after_widget' => '</div>',
    'before_title' => '<h2>',
    'after_title' => '</h2>',
  ));

}

Is this even possible or do I have to delete my class "widgets" and adding new classes everytime to the new widget?

4

When registering a sidebar, you can use certain placeholders for the widget classes. Here's an example taken from the codex:

'before_widget' => '<li id="%1$s" class="widget %2$s">',

It's the lack of %1$s and %2$s that is causing your issues. When in doubt always read the documentation on the codex and the developer hub/handbooks

0

How about

function create_widget($name, $id, $description) {

  register_sidebar(array(

'name' => __( $name ),

    'id' => $id,
    'description' => __( $description ),
    'before_widget' => '<div class="widget widget_' . $name . '" >',
    'after_widget' => '</div>',
    'before_title' => '<h2>',
    'after_title' => '</h2>',
  ));

}

This way you will have the widget class wrapper for your widgets and also the widget_$name class, that you use from CSS. Or you can completely remove the widget CSS class if you plan to do that, and everything looks great when you test.

Make sure you understand what CSS styles you will loose after you remove the widget class. But this is possible.

-1

To add another sidebar you simply need to register another instance. If you are registering more than one, then you will also need to name them. Change your functions.php file to look like the following. Take care to add in the extra line that specifies the name of your sidebar. Once these are functions are defined, you will notice the extra sidebar appear in the WordPress Dashboard under the Appearance > Widgets option. It’s here that you can drag and drop all your widgets into your various sidebars.

'sidebar 1', 'before_widget' => '', 'after_widget' => '', 'before_title' => '

', 'after_title' => '

' )); register_sidebar(array( 'name' => 'footer sidebar 1', 'before_widget' => '', 'after_widget' => '', 'before_title' => '

', 'after_title' => '

' )); }?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.