michel lompret michel lompret - 4 months ago 7
PHP Question

Add An If in A Foreach Loop

I'm trying to add part of a

foreach loop
on a condition like this:

foreach ( $repeater_val as $key => $row_values ) {
$number = $row_values['number'];
// Get the 'selector' value
$selector = $row_values['selector'];
$typ = $row_values['cheky'];
// DO SOMETHING WITH THOSE VALUES HERE
if ( class_exists( 'WooCommerce' ) {
$seleci = $row_values['summit'];
$penard= woocommerceCategorySlug($seleci);
// DO SOMETHING WITH THOSE VALUES HERE
$woo = array(
'posts_per_page' => $number,
'post_type' => 'product',
'product_cat' => $penard,
'meta_query' => array(
array(
'key' => '_thumbnail_id'
)
)
);
}


But I got an error:

Parse error: syntax error, unexpected '{' for the *if ( class_exists( 'WooCommerce' ) {*


Is there a way to add an if in a foreach loop ?

Answer

You just forgot to close your bracket on the conditional

Replace your line with this if ( class_exists( 'WooCommerce' ) ) {

Comments