Hi @sitenerds,
I hope you are doing well today!
To modify the code to support multiple category IDs, you can use an array of category IDs and then check if the selected category ID is in that array.
<?php
add_action(
'wp_head',
function() {
global $post;
if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) {
return;
}
$form_id = 317;
// Use an array for multiple category IDs
$cat_ids = [50, 51, 52]; // Example: Adding categories 50, 51, and 52
$cond_field = 'number-2';
?>
<script type="text/javascript">
jQuery(document).bind("ready ajaxComplete", function(){
// Convert PHP array to JavaScript array
var catIds = <?php echo json_encode($cat_ids); ?>;
if(catIds.includes(parseInt(jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-hp_listing_category"]').val()))) {
jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden");
} else {
jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden");
}
jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-hp_listing_category"]').bind("select2:select", function(e){
if(catIds.includes(parseInt(jQuery(this).val()))) {
jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden");
} else {
jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden");
}
});
});
</script>
<?php
}
);
- The
$cat_id
variable is replaced with $cat_ids
, which is an array of category IDs ([50, 51, 52]
).
- In the JavaScript part, we have used
var catIds = <?php echo json_encode($cat_ids); ?>;
to pass the PHP array to JavaScript. This creates a JavaScript array with the same values.
- To check if the selected category is in the array, we have used
catIds.includes(parseInt(jQuery(this).val()))
. This checks if the value from the select field (converted to an integer for safe comparison) is in the catIds
array.
- The rest of the code remains largely the same, adjusting only for the fact that you’re now checking against multiple values instead of just one.
This approach allows you to easily add or remove category IDs from the $cat_ids
array without having to significantly alter your JavaScript logic.
Kind regards,
Zafer