WP: Advanced Custom Fields. Flexible Content + Repeater Fields. Using Custom fields in inline CSS

I am trying to style a H4 element with a left and bottom border of varying widths and a specific colour which is defined by a custom field that the client would fill in the CMS. My CMS layout is correct but the code below I know not to be. My problem area is specifically with the H4, the other parts of the code are rendering correctly.

<?php               
if (get_field("sponsorlogos")){
  while (has_sub_field("sponsorlogos")){

    if (get_row_layout() == "sponsor_category_title"){ // Sponsor Category Title
      echo '<h4 style="border-left: 10px solid '.the_sub_field('sponsor_category_title_colour').' !important; border-bottom: 1px solid '.the_sub_field("sponsor_category_title_colour").' !important; margin: 40px 0 15px 0;">';
        the_sub_field("sponsor_category_title_text");  
      echo '</h4>';
    }  

    if (get_row_layout() == "sponsor_logo_container"){ // Sponsor Repeater Field
      $rows = get_sub_field('sponsor_item'); 
      if ($rows){ 
        echo '<ul id="sponsor-logo-container">';
        foreach($rows as $row){
          echo '<li class="sponsor-logo"><a class="logo-link" target="_blank" href="'.$row['sponsor_link'].'"><img width="190" height="110" src="'.$row['sponsor_logo'].'" alt="'.$row['sponsor_title'].'"class="logo-image grayscale" /></a><span class="staff-name">'.$row['sponsor_support'].'</span></li>'; 
        }
        echo '<div style="clear:both;"></div>';
        echo '</ul>';
      }
    } 
  }
}
?>

The resulting markup is as follows:

<h4 style="border-left: 10px solid  !important; border-bottom: 1px solid  !important; margin: 40px 0 15px 0;">Bronze</h4>

You can see it omits the hex '#xxxxxx' colour part.

I should reiterate though, as the colour hex is successfully spat out twice in the markup (as expected) but it appears above the H4 element.

Im sure I'm not using the Advanced Custom Field tags correctly.

Help!

Solutions

Try changing the PHP code from

echo '<h4 style="border-left: 10px solid '.the_sub_field('sponsor_category_title_colour').' !important; border-bottom: 1px solid '.the_sub_field("sponsor_category_title_colour").' !important; margin: 40px 0 15px 0;">';

to

echo '<h4 style="border-left: 10px solid '. get_sub_field('sponsor_category_title_colour').' !important; border-bottom: 1px solid '.get_sub_field("sponsor_category_title_colour").' !important; margin: 40px 0 15px 0;">';

In other words, replace the_sub_field() with get_sub_field(). You should use the get_sub_field() function when you already have an echo call. You use the_sub_field() when there is no echo preceding it.

Similar questions

WP: Advanced Custom Fields - flexible content link formatting
I'm using the Flexible Content add-on for Advanced Custom Fields and having trouble formatting a simple button link in my template code. Normally I'd use something like this... ...to output the following html: But I can't figure out how to adapt that to work within my flexible content template: Any suggestions? Thanks in advance
Advanced Custom Fields Flexible Content with Multiple Bootstrap Collapse (Accordion)
I'm using ACF Flexible Content to allow multiple accordions on the same page. The problem for me is the counting integer. With each new accordion, it starts counting the sections ('#collapse-#') over again. I'm not too PHP savvy. My code is returning this: Accordion 1 Accordion 2 But, I need it to print this: Accordion 1 Accordion 2 My code so far:...
Advanced Custom Fields. Flexible Content with Gallery subfield
I am trying to build a Flexible Content which contains 4 possible Layouts (a description, a hero image, a gallery 1col and a gallery 2col). So far I can get the Description and Hero Image subfields to display just fine but the Gallery subfield wont show up on the page. Don't know what I'm doing wrong. Here's the code:
use add_row to programmatically add an entry to a flexible content field in advanced custom fields for wordpress
Wondering how to use add_rows (or something similar) to add an entry to a flexible content field programmatically. On ACF's website they mention add_rows can be used to add a row to a flexible content field https://www.advancedcustomfields.com/resources/ but they give no examples how to do it with a flexible content field; only with a repeater fiel...
Wordpress Advanced Custom Fields - Advanced Repeater
So I working on a wordpress site with the FoundationPress Theme and I recently upgraded ACF to the Pro version which allows for the repeater field. So I'm trying to allow the client to add new social media accounts with the repeater. http://imgur.com/FT58SpD http://imgur.com/MydpAwl Above you can see how the repeater is set up on the backend. Heade...
How to create single-custom.php for different repeater fields (using Advanced Custom Fields) in Wordpress
I'm using Advanced Custom Fields Pro with Wordpress 4.1.1 I feel like this should be pretty easy but I've been struggling with it. The example I'll start with is I have a "Team" page that has a repeater with: All as sub_field I have everything working nicely in the page where all posts are display (the actual Team landing page) but I can't figure o...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy .