Support

Account

Home Forums Feedback Directly get a layout by its ID

Solved

Directly get a layout by its ID

  • Hi,

    Since, ACF only support a shortcode for text_fields (as mentioned on the documentation https://www.advancedcustomfields.com/resources/shortcode/), I’m developing my own shortcode to get my fields into my content.

    For instance, for my posts/pages, I’ve created a Flexible Content. It contains stuffs like gallery and specifics layouts for my needs. When I’m writing an article, I can then create any amount of layouts I need and place them the way I want into my article content.
    Example: [taz_shortcode id=2] would call the layout #2 from my Flexible Content.

    All of this is working, but I’m using the “while” thingy to loop through the layouts list for each of my shortcode and compare the provided id to the one of the layout. This is working but to me, it’s not optimized at all.

    How can I directly target the layout id I need in my PHP function without looping every single time to the layout list?

    Thanks for your help.

  • There are few ways you can do this, the first is not really optimized, but it will shorten the work.

    
    $flex = get_field('flexible_content_field');
    

    That will put the entire contents of the flex field into an array and then you can get the row by targeting the array element you need

    
    // the array elements start with 0 instead of 1
    // so row 2 will be element 1 of the array
    $text = $flex[1]['text_field'];
    

    You will need to output the contents of the array to be 100% certain what it contains, and it will give you a better idea of what to do with it.

    
    echo '<pre>'; print_r($flex); echo '</pre>';
    

    While not 100% optimized, because you are getting all of the rows at once, the values are cached so additional calls to get the entire flex content should not generate additional queries to the DB.

    The second way is to only get what you want using get_post_meta()

    For example, a text field on the second row will have a meta key that looks something like this repeater_field_name_2_text_field_name. The meta key repeater_field_name will return a array of the layouts for each row.

    Like I said, more complicated, but you only get what you need.

  • Excellent John.
    I used the first method, it’s per-fect!
    Thank you very much, appreciated.

  • Hello,

    would you mind sharing your code as I need to do something similar.
    It would be very helpful.

    Thanks

  • function taz_get_field_data($shortcodeID){
    	if (class_exists('acf')){
    		$layouts = get_field('taz_shortcodes');
    		return $layouts[$shortcodeID - 1];
    	} else {
    		error_log("ACF Plugin is not available.");
    		return null;
    	}
    }
    
    function taz_shortcode($atts, $content = null){
    	$a = shortcode_atts(array(
    		'id' => null
    		), $atts);
    
    	$shortcodeID	= (int)$a['id'];
    	$rowData		= taz_get_field_data($shortcodeID);
    
    	switch ($rowData['acf_fc_layout']){
    		case 'gallery': // your layout name
    			// Do you stuff
    			break;
    		case 'before_after':
    			// Do you stuff
    			break;
    		case 'rewind_forward':
    			// Do you stuff
    			break;
    		default:
    			break;
    	}
    }
    add_shortcode('taz_shortcode', 'taz_shortcode');
  • Thanks.

    very helpful
    🙂

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Directly get a layout by its ID’ is closed to new replies.