I have different widget areas. One is a horizontal area that may contain eg. 4 widgets next to each other. My Question is how would i add a different eg. $param['widget_title']
to widget number $n
(via a filter)?
I already tried using wp_registered_widgets();
function and te different globals, but with no result.
The
dynamic_sidebar_params
filter is called before each widget is displayed, so multiple times in the same sidebar. It filters the$args
and$instance
parameters that will be passed to the widget.If you only want to execute it for the third widget of a specific sidebar, you should check for the sidebar ID (found in the
id
value of the$args
array, which is passed first), and then count the widgets until you arrive at n. There are different ways to do this: you can get all widgets of the current sidebar and compare the ID of the n-th with the one you are filtering now, or you can just count it in your filter, resetting the count when you get a new sidebar name. Or you could replacedynamic_sidebar()
in your template with a call to a function of your own that will first reset the widget count and then start filtering.This is an example of the second type: counting widgets in the function, resetting the count each time we start a new sidebar:
This way everything works… but take a look at Jans approach.
That’s the working code: