Support

Account

Home Forums General Issues Calling value from functions file

Solved

Calling value from functions file

  • I have searched the forums and documentation for this — I feel like it’s really very simple, but nothing’s working, so I’ll try here:

    I have defined a color picker field in my ACF settings (if it makes any difference, it’s within a flexible content field). Normally, I would write this value into the loop using inline style, but for various reasons I want to put it into the head, inside <style type="text/css"></style>.

    I cannot figure out what I’m doing wrong. My field’s name is item_color, and this is what I’ve written into my functions.php file:

    <?php 
    function acf_css() {
    $color = get_field('item_color', 'options');
    ?> 
    <style type="text/css"> .item:hover { color: <?php echo $color ?>; } </style>'; 
    <?php
    }
    add_action( 'wp_head', 'acf_css');

    Well, I’ve actually tried several things, this is just the latest. For everything I’ve done, including this, firebug shows the inline style for the item, but the value isn’t showing up, so it just says “.item:hover { color: }”

    I’m sure it’s so obvious — what am I doing wrong?

  • if the color picker field is within a flexible content field (a sub field of the flex field) then you’ll need to loop through the layouts of the flex field to get the value of the field https://www.advancedcustomfields.com/resources/flexible-content/

    
    if (have_rows('flexible_content_field_name', 'options')) {
      while(have_rows('flexible_content_field_name', 'options')) {
        the_row();
        $color = get_sub_field('item_color');
        // loop continues
      }
    }
    

    The main problem here is… if you have multiple layouts, which layout does the color come from? See the documentation I linked to, more than likely you’re looking for a specific layout?

  • There it is! I do have multiple layouts, but I was able to figure that out. Genius — thank you so much!

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

The topic ‘Calling value from functions file’ is closed to new replies.