Support

Account

Home Forums General Issues Custom post loop and ACF fields issue

Solved

Custom post loop and ACF fields issue

  • I am having issues with the following custom post type loop. The fields in the DIV are being stripped from the HTML and displayed above it. Any ideas why this is happening? Thanks.

    add_shortcode( 'hotdeals_shortcode', 'special_vehicles_custom_post_type' );
    function special_vehicles_custom_post_type() {
      $args = array(
        'post_type' => 'special_vehicles',
        'post_status' => 'publish',
        'posts_per_page' => 1,
        'category_name' => 'Hot Deals',
      );
      $string = '';
      $query = new WP_Query( $args );
        
      if($query->have_posts()) {
          
        while($query->have_posts()) {
          $query->the_post();
          $string .= '<div class="overlay-bg">';
          $string .= '<p class="description">'.get_the_title().'
    ';
          $string .= '<p class="description">'.the_field('mileage').'
    ';
          $string .= '<p class="price">'.the_field('price').'
    ';
          $string .= '</div>';
        }
          
      } else {
        echo 'Currently no hot deals to display.
    ';
      }
      wp_reset_query();
      return $string;
    }
  • It may be just about closing the p tags. Give that a try. Or wrap them in spans with br’s perhaps?

  • Hello,

    It looks like the previous paste of the code had the closing p tags stripped out. I have reviewed my code and this is not the problem. WordPress or ACF just does not like having the fields displayed in the loop for some reason. It works in the loops above. Any ideas?

  • Hi @mikehermary

    Do you have a link where I could see it in action, so I could look at the source HTML?

    It feels like an HTML/CSS issue more than a WordPress/ACF issue but I will have a quick look.

    It works for me when I duplicate it… so it might be something more specific with your setup. Perhaps activating a different theme briefly to see if it’s the theme.. then go from there?

  • Hello,

    The website is in staging, so I cannot provide a link. I have included a link to the homepage source for you to review: Source on JSFiddle. Search for ‘<!– Hot Deals widget, shortcode and custom post loop issue –>’ to find the start of the widget. You will see the two instances of 40,000 are outside of the widget container.

    Thanks.

  • Oh! Ooops… it’s the use of the_field as opposed to get_field when assigning the value to a variable. I should have caught that sooner.

  • Hello,

    That was the problem. Thanks for your assistance. Have a good day.

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

The topic ‘Custom post loop and ACF fields issue’ is closed to new replies.