update

My widget was set to output divs, not li elements, and so the $icons_array was useless. I fixed the issue by altering the <li portion to <div

appreciate your solutions


I've got a function that shows user designated quantity of icons inside a random order. The variable $widgetnumber should control the amount of icons to exhibit, however it it isn't working. Regardless of what number is joined, all icons are displayed, although if is joined, no icons are displayed, so a few of the logic is working.

I'd like the default to become 1 widget showing.

I have echoed the variables and they're being populated properly. Does anybody see anything that could be leading to this to not work as referred to above?

Thanks!

Complete function:

// Sidebar widget limit
function limit_widgets($sidebar){
    ob_start();
    $widgets = dynamic_sidebar($sidebar);
    if($widgets){
        $html = ob_get_contents();
        $widgets_array = explode("<li",$html);
        $widgets_end = count($widgets_array);
        $body = array_slice($widgets_array, 0, $widgets_end+1); 
        shuffle($body);
        global $wp_query;
        $postid = $wp_query->post->ID;
        $widgetnumber = intval(get_post_meta($postid, 'widget_number', true));

        var_dump($widgetnumber);
        var_dump($body);

        for ($i = 0; isset($body[$i]) && $i < $widgetnumber; $i++) {
            $final_html .= $i.' '.$body[$i];
        }
    }
    else{
        $final_html = "";
    }
    ob_end_clean();
    echo $final_html;
}

The HTML output is:

0
    <div class="ad-feature">
        <a class="widget_sp_image-image-link" target="_blank" href="http://www.toysrus.com/shop/index.jsp?categoryId=2255956" target=""><img alt='' src="https://.../wordpress/wp-content/uploads/2012/01/ad.jpg" style="" /></a>
    </div>

    <div class="ad-feature">
        <a class="widget_sp_image-image-link" target="_blank" href="http://jcp.is/JCPMerchTab" target=""><img alt='JCPenny' src="http://.../wordpress/wp-content/uploads/2012/01/Tshirtad2.png" style="" /></a>
    </div> 

Furthermore, placing var_dump($widget); and var_dumb($body) don't output anything.

Try changing your foreach with this particular and find out whether it does what you would like:

for ($i = 0; isset($body[$i]) && $i < $widgetnumber; $i++) {
    $final_html .= $i.' '.$body[$i];
}