I have a filter for the CPT and I need to manually type all custom taxonomy names inside the array():
sc_render_filter(
'test',
'All Categories',
array( 'Category One', 'Category two', 'Category three'),
''
. ($current_sub_brand ? ( 'sub_brand=' . $current_sub_brand . '&' ) : '' )
. ($current_varietal ? ( 'varietal=' . $current_varietal . '&' ) : '' )
. ($current_publication ? ( 'publication=' . $current_publication . '&' ) : '' )
. ($current_vintage ? ( 'vintage=' . $current_vintage . '&' ) : '' )
);
Is it possible somehow to use the variable or foreach loop inside array() to automatically generate terms or names? Or maybe I need another approach?
This is what I have in foreach:
$source = '';
foreach ($termslist as $term) {
$source .= "'". $term->name. "'". ',';
}
echo rtrim($source, ',');
My Answer:
Yes, you can use a foreach loop to dynamically generate the terms or names and then insert them into the array. Here's an example of how you can do that:
php
$termslist = array('Category One', 'Category Two', 'Category Three'); // This is just an example, you can replace it with your actual array of terms
$categories = array();
foreach ($termslist as $term) {
$categories[] = $term;
}
sc_render_filter(
'test',
'All Categories',
$categories,
''
. ($current_sub_brand ? ( 'sub_brand=' . $current_sub_brand . '&' ) : '' )
. ($current_varietal ? ( 'varietal=' . $current_varietal . '&' ) : '' )
. ($current_publication ? ( 'publication=' . $current_publication . '&' ) : '' )
. ($current_vintage ? ( 'vintage=' . $current_vintage . '&' ) : '' )
);
This way, the terms or names will be dynamically generated based on the contents of the `$termslist` array.
Rate this post
3 of 5 based on 2308 votesComments