I can't provide you with any code since I not really know where to start.

I like to add some programmatically created widgets (or just html) after the 1st Widget inside my sidebar. Right now I have it that I have to include this widgets before all others, that was pretty easy of course.


If I'm understanding you correctly, a dirty way to achieve this would be to use 2 sidebar areas on the page: an upper and lower. Call the upper, then include your code, then call the lower.

Other ways might be to create your own widget that pulls in the appropriate data at runtime, but you haven't explained the "programmatically created" part of your requirement.


Edit: Forgot - add this code to your functions.php

function nb_widget_number( $params ) {
    $widget_id = $params[0]['id'];
    $widget_array = wp_get_sidebars_widgets();
    $widget_count = count( $widget_array[$widget_id] );

        // set widget number to array if not set already    
    if( !$nb_widget_num )
        $nb_widget_num = array();

        // if no widgets, return params
    if( !isset( $widget_array[$widget_id] ) || !is_array( $widget_array[$widget_id]) )
        return $params;

        // if widget number set, add 1, if not, set to 1             
    isset( $nb_widget_num[$widget_id] ) ? $nb_widget_num[$widget_id] ++ : $nb_widget_num[$widget_id] = 1;

    // now you can do anything with that count you want. in this case, place something after the nth widget (1st for this example)
        if( $nb_widget_num[$widget_id] == 1 )
            $params[0]['after_widget'] = 'SOME CODE';

        return $params;

Then you just need to add the filter to let wordpress know to use this function:

add_filter( 'dynamic_sidebar_params', 'nb_widget_number');

In addition to adding code after the widget, you can use this to add code, modify classes (such as first/last), or really change any feature about the widgets $params.

  • I'm not sure this addresses the question... – vancoder Sep 3 '13 at 21:05
  • How does it not address the question? They want to add code after the 1st widget? This counts the widgets in a given sidebar and modifies the widget params to add any code after the widget they want. Am I missing something? – NickJAB Sep 3 '13 at 21:07
  • I not get the ` if( !$nb_widget_num )` line, why should it be set? there is no global or something b4 that could possible set it. When I remove that line it sort of works but not really what I needed, it puts this code inside every widget, I like it to be included after the entire widget. Anyway my [n]th was also confusing and maybe even wrong. I think @tschos answer in the duplicate might be exactly what I am looking for - looking into it now. Thanks anyway. – David Icke Sep 3 '13 at 22:24
  • You could remove it (I think it was in my code for another reason and I didn't catch the removal) but the idea is to set it as an array. Anyway, I won't bother to go into it further if you have found a solution. Good luck. – NickJAB Sep 3 '13 at 22:28

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