Support

Account

Home Forums General Issues Post object with conditions performance, elsif or switch or separate blocks

Helping

Post object with conditions performance, elsif or switch or separate blocks

  • I’m looking for some community suggestions and help. How would you optimize this post object with conditions?

    My concern is once conditions will grow to 10 and post object is 10-20 it will cause performance issues as it has to render through conditions before printing the custom field value from the post object.

    My idea is either to go for conditions or just create different blocks instead of conditions and more raw code them without conditions.

    What’s your guys thoughts? The maintenance of the code is more or less the same. Conditions would be a little better but also make the code more complex.

    I’ve been testing elseif vs switch so far. While I’ve seen switch perform much better as soon as conditions grow over 4-5 conditions.

    My questions:
    How would you have written this? Can it be optimized more?
    Would you use elseif or switch? If elsif would you add the default in the top as well as if match it will pass value faster for the most common condition? If switch would you do the same?

    Would you go for conditions or just 10 different acf gutenberg blocks instead?

    Core information:
    – The post object is fetched from an options page.
    – The post object will be ranging from 10-20+
    – The post objects are multilingual but doesn’t matter in the code case but worth to mention.
    – Conditions is defined on edit page by a select field.
    – Conditions will grow up to 10 in the long run.
    – The code example here is just one out of several values fetched. And there will be minor conditions within fields as well if empty or if conditions 2 echo this field instead.

    Post object fetch an image with switch:

    <?php
    
    $conditions = get_field( 'conditions_category' );
    
    $post_object = get_field('post_object_categoryone_1','options'); // Set field to grab
    if( !empty ($post_object) );
            $imageArray_BG = get_field('post_hero_img_1', $post_object->ID); // Array returned by Advanced Custom Fields
    		$imageAlt_BG = esc_attr($imageArray_BG['alt']); // Grab, from the array, the 'alt'
    		$imageURL_BG = esc_url($imageArray_BG['url']); // Grab the full size version
    
    $post_object_2 = get_field('post_object_categorytwo_2','options'); // Set field to grab
    if( !empty ($post_object_2) );
            $imageArray_BG_2 = get_field('post_hero_img_1', $post_object_2->ID); // Array returned by Advanced Custom Fields
    		$imageAlt_BG_2 = esc_attr($imageArray_BG_2['alt']); // Grab, from the array, the 'alt'
    		$imageURL_BG_2 = esc_url($imageArray_BG_2['url']); // Grab the full size version
    
    	switch ($conditions){
    		case "value1":
    			echo '<img src="' .$imageURL_BG_2. '" alt="' .$imageAlt_BG_2. '" class="img-example">';
    			break;
    		
    		case "value2":
    			echo "Value 2";
    			break;
    		
    		case "value3":
    			echo "Value 3";
    			break;
    		
    		case "value4":
    			echo "Value 4";
    			break;
    		
    		default:
    			echo '<img src="' .$imageURL_BG. '" alt="' .$imageAlt_BG. '" class="img-example">';
    	}
    
    // always good to see exactly what you are working with
    
    //echo '<span style="font-size: 11px; font-weight: 400; font-family:andale mono">';
    //var_dump($conditions);
    //echo '<br>';
    //var_dump($imageURL_BG);
    //echo '<br>';
    //var_dump($imageURL_BG_2);
    //echo '<br>';
    //echo '</span>';
    
    ?>
    

    Post object fetch an image with elsif:

    <?php
    
    $conditions = get_field( 'conditions_category' );
    
    $post_object = get_field('post_object_categoryone_1','options'); // Set field to grab
    if( !empty ($post_object) );
            $imageArray_BG = get_field('post_hero_img_1', $post_object->ID); // Array returned by Advanced Custom Fields
    		$imageAlt_BG = esc_attr($imageArray_BG['alt']); // Grab, from the array, the 'alt'
    		$imageURL_BG = esc_url($imageArray_BG['url']); // Grab the full size version
    
    $post_object_2 = get_field('post_object_categorytwo_2','options'); // Set field to grab
    if( !empty ($post_object_2) );
            $imageArray_BG_2 = get_field('post_hero_img_1', $post_object_2->ID); // Array returned by Advanced Custom Fields
    		$imageAlt_BG_2 = esc_attr($imageArray_BG_2['alt']); // Grab, from the array, the 'alt'
    		$imageURL_BG_2 = esc_url($imageArray_BG_2['url']); // Grab the full size version
    
    if($conditions == 'value1') {
     	echo '<img src="' .$imageURL_BG_2. '" alt="' .$imageAlt_BG_2. '" class="img-example">';
    }
    elseif($conditions == 'value2') {
     	echo "Value 2";
    }
    elseif($conditions == 'value3') {
            echo "Value 3";
    }
    elseif($conditions == 'value4') {
            echo "Value 4";
    }
    else{
    	echo '<img src="' .$imageURL_BG. '" alt="' .$imageAlt_BG. '" class="img-example">';
    }
    
    // always good to see exactly what you are working with
    //echo '<span style="font-size: 11px; font-weight: 400; font-family:andale mono">';
    //var_dump($conditions);
    //echo '<br>';
    //var_dump($imageURL_BG);
    //echo '<br>';
    //var_dump($imageURL_BG_2);
    //echo '<br>';
    //echo '</span>';
    
    ?>
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.