The scenario is as follows: the user marks "checkboxes" options in the corresponding meta-field in the admin panel and the output (depending on the option chosen) is executed shortcode.

I wrote the php-code myself, but it gets bulky. I would like to clarify how to optimize it correctly. Here is an abstract example for one option:

if ( in_array( 'Чай', get_field('meal_type') ) ) { the_content(); do_shortcode("[tea]"); } 

Everything here works and information is displayed as needed. But if there are many such fields, then the code is cumbersome (if described separately).

How to immediately combine the necessary categories of meta-fields in the selection and execute the appropriate shortcodes for them and display the information after the_content() function?

For example:

 the_content(); array(){ 'Чай' -> do_shortcode['tea'], 'Кофе' -> do_shortcode['coffee'], ... } 
  • Well, get the names of the fields from the database, why do you manually write them in the code? - Jean-Claude

1 answer 1

I would do something like this:

 $meals = array( 'Чай', 'Кофе', 'Вино', 'Пиво' ); $meal = get_field( 'meal_type' ); if ( in_array( $meal, $meals, true ) ) { the_content(); echo do_shortcode( "[$meal]" ); } 
  • Thanks for the thought! I will try to push off from this decision. - Tesla
  • If the answer is useful, it is worth noting as accepted by a check mark below. This will help navigate users who are looking for the answer to a similar question. - KAGG Design