• Resolved Omnhio

    (@omnhio)


    Hi all, I’m trying to figure out how to customize the output render of widgets inside a certain dynamic sidebar. I’ve got the following code to register my custom sidebar:

    register_sidebar( array(
    		'name' => 'Frontpage Widgets',
    		'id' => 'frontpage-widgets',
    		'description' => '',
    		'before_widget' => '<div class="col">',
    		'after_widget' => '</div>',
    		'before_title' => '<h3 class="widgettitle">',
    		'after_title' => '</h3>',
    	));

    Then, I’ve got the render of that sidebar somewhere in my template:

    <?php dynamic_sidebar('frontpage-widgets'); ?>

    Once I’ve put some widgets inside that sidebar, from the backend of WordPress, how can I customize/change the render of this specific sidebar?
    Which hook should I use or override to achieve that?

    I’d like, for istance, to render all widgets within that sidebar in groups of three. Maybe wrapped in a <div class="row">.

    Thanks to all!

Viewing 2 replies - 1 through 2 (of 2 total)
  • not tested, this might give you some hooks to hook into to add/change the ‘before_widget’ and ‘after_widget’ parameters;

    https://www.ads-software.com/support/topic/how-to-first-and-last-css-classes-for-sidebar-widgets?replies=9

    Thread Starter Omnhio

    (@omnhio)

    Thanks a lot! Here’s my approach:

    Functions

    if (!function_exists('groups_frontpage_widgets')):
    function groups_frontpage_widgets($params) {
    	$id = $params[0]['id'];
    	static $counter = 0;
    
    	if ('frontpage-widgets' !== $id) return $params;
    
    	if ( 0 !== $counter && 0 === $counter % 3 )
            $params[0]['before_widget'] = '</div><div class="row">'. $params[0]['before_widget'];
    
    	$counter++;
    
    	return $params;
    }
    endif;
    
    add_filter('dynamic_sidebar_params', 'groups_frontpage_widgets');

    In the template

    <?php // Display Frontpage Widgets
    	if(is_active_sidebar('frontpage-widgets')) : ?>
    		<div id="frontpage-widgets">
    			<div class="row">
    					<?php dynamic_sidebar('frontpage-widgets'); ?>
    				</div>
    		</div>
    	<?php endif; ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Customize the render of widgets in a certain dynamic sidebar (by id)’ is closed to new replies.