0

I would like to break up a sidebar over three containers. Each container should hold up to three widgets of the sidebar. Therefore, I need to find the HTML for only the widgets 0 to 2, 3 to 5 and 6 to 8 separately.

Normally, I would use dynamic_sidebar() to display the sidebar, but this function doesn't accept parameters to specify that I only want a range of the widgets of the sidebar. Moreover, I found wp_get_sidebars_widgets(), but it is not intended to use for themes.

How can I get only the widgets in a given range of a dynamic sidebar? For example, this is what I think of.

// functions.php
// display range of widgets of a sidebar
function dynamic_sidebar_range($name, $from, $to)
{
    $widgets = get_dynamic_sidebar($name); // this function doesn't exist!
    for ($i = $from; $i < count($widgets) && $i < $to; ++$i ) {
        $widget = $widgets[$i];
        echo $widget['before_widget']
           . $widget['before_title']
           . $widget['title']
           . $widget['after_title']
           . $widget['content']
           . $widget['after_widget'];
    }
}

// frontpage.php
// consists of blocks of three posts or widgets alternatingly
for ($sections = 0; $sections < 3; ++$sections) {
    // display three posts
    for ($i = 0; $i < 3 && have_posts(); ++$i) {
        the_post();
        // ...
    }
    // display three widgets
    $from = $sections * 3;
    $to = $from + 3;
    dynamic_sidebar_range('name', $from, $to);
}
  • Display based on what? Try conditional tags. Please try and be more specific with your question as its currently far too broad. – Brad Dalton Feb 22 '14 at 10:19
  • @BradDalton I want to display the first three widgets of a given dynamic sidebar in one HTML container, the next three widgets in another, and so on. I added a code example to make it more clear. – danijar Feb 22 '14 at 10:21
  • 2
    Silly question: why not just register three sidebars? – Chip Bennett Feb 22 '14 at 13:18
  • @ChipBennett I considered that, but I am interested of how I could split the widgets automatically. – danijar Feb 22 '14 at 14:34
  • Seems like overkill to me. Much easier just to register 3 sidebars, and then instruct users to add 1-3 Widgets to each one. – Chip Bennett Feb 22 '14 at 14:57
0

If what you want is "to break up a sidebar over three containers", then you don't really need to rebuild dynamic_sidebar() to do it. There is a filter called dynamic_sidebar_params that can be leveraged to break up the sidebar.

function add_closing($params) {
  $params[0]['after_widget'] = $params[0]['after_widget'].'</div><!-- close wrapper-thing -->';
  return $params;
}

add_filter(
  'dynamic_sidebar_params',
  function ($params) {
    static $c = 0;

    if (0 === $c || 0 === $c%3) {
      $params[0]['before_widget'] = '<div class="wrapper-thing" style="border:1px solid white;float:left">'.$params[0]['before_widget'];
      remove_filter('dynamic_sidebar_params','add_closing');
    } else {
      add_filter('dynamic_sidebar_params','add_closing');
    }

    $c++;
    return $params;
  }
);

I found this interesting in a "code golf" sort of way. I am fairly sure that three distinct sidebars, as suggested in comments, is a better way to do this.

This will wrap the widgets in a sidebar in distinct containers, three per container. If you want distinct widget areas what you are doing is entirely the wrong to go about things. It is going to be more confusing than having individual sidebars and it will remove control from the end user. That is not to mention that you will need to largely recreate Core code.

Just use the sidebar system as it was intended.

  • Thanks for your answer. I'm not sure if I understand it entirely though. Does this just add container tags every three widgets? I need to break it up into containers that are at different locations in the HTML structure. But if there is a way to do this by hooking into Wordpress events, that would be fine. – danijar Feb 23 '14 at 12:28
  • @danijar : see the edit – s_ha_dum Feb 23 '14 at 14:42

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.