Okay
  Public Ticket #4146287
acf fields integration
Closed

Comments

  •  1
    Iyad started the conversation

    Hello,

    i would like to show an acf field in the loop-markup.php

    When i use something like the following code in the content div, the new div is not shown an the field appears outside the grid item (screenshot attached)

    $markup .= '<div class="test">'; 

    $markup .= the_field('alter'); 

    $markup .= '</div>';

    What should i do to put an acf field as php code inside the content div?

    Thanks for your suppourt

    Attached files:  Bildschirm­foto 2025-07-13 um 12.49.50.png

  •  352
    Pat replied

    Hi Iyad,

    Thank you for writing to us.

    This is happening because the the_field() function in ACF immediately prints the value to the page.

    To fix this, you need to use get_field() instead, which returns the value so you can add it to your markup variable correctly.

    Please replace your code with this:

    $markup .= '<div class="test">';
    $markup .= get_field('alter');
    $markup .= '</div>';
    

    Hope this does the trick!

     

    Best wishes,

    Salient Theme Support

  •  1
    Iyad replied

    Hello Noah,

    thanks for your help. Now i get the „Warning: Array to string conversion“. The ACF Field is a choice Field. How can i get the selected Value instead of the whole array? 

    it works fine now for text fields

    Best Regards

  •  9,391
    Tahir replied

    Hey Again,

    Thanks for reaching out! .

    The "Post Loop Builder" already has a Custom Field feature that you can use . See screenshot:  9806008243.png

    Best,

     


     Salient Support Team 


  •  1
    Iyad replied

    Thank you Tahir! But i still would like to write a flexible code in the loop-markup.php, so it would be perfect to get the selected Values instead of the whole array from AFC in the loop-markup.php.

    Best Regards

  •  9,391
    Tahir replied

    Hi Again,

    To get only the selected values from an ACF field in loop-markup.php, use get_field() and ensure the return format for that field in ACF settings is set to "Value" instead of "Label" or "Both". This ensures you receive just the raw selected values.

    If the field allows multiple selections, the result will be an array. You can work with individual values from it or just pick the first one if needed. If the field is a single-select, you'll get a single value directly.

    For more complex fields like repeaters or groups, the structure is different. You’d access individual subfields or loop through rows depending on the setup.

    Thanks


     Salient Support Team 


  •  1
    Iyad replied

    Thanks! I found the solution now. it looks like he didn't know which ID to use,  so i used the funktion
    $post_id = get_the_ID();
    and then added the post_id to the field:
    $markup .= get_field('alter', $post_id);


  •  9,391
    Tahir replied

    Glad to help! .

     


     Salient Support Team