0

I'm trying to display an ACF field within a Wordpress loop using functions.php to create a shortcode. Unfortunately, the_field('conference_location') does not display.

function show_conferences_func() {
  global $post;
    $html = "";
    $my_query = new WP_Query( array(
    'post_type' => 'product',
    'posts_per_page' => 3,
    'product_cat' => 'conferences'
  ));
  if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();
    $html .= "<div class=\"conference-block\">";
    $html .= "<h2>" . get_the_title() . " </h2>";
    $hmtl .= "<p>" . the_field('conference_location') . "</p>";
    $html .= "<a href=\"" . get_permalink() . "\" class=\"button\">Learn more</a>";
    $html .= "</div>";
  endwhile; endif;

  return $html;
 }
 add_shortcode( 'show_conferences', 'show_conferences_func' );

1 Answer 1

0

You should use get_field('conference_location') instead of the_field('conference_location')

the_field() echo your data. So you're not storing your value.

Sign up to request clarification or add additional context in comments.

1 Comment

Like this? I tried this, but had no luck: if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post(); $html .= "<div class=\"conference-block\">"; $html .= "<h2>" . get_the_title() . " </h2>"; $hmtl .= "<p>" . get_field('conference_location') . "</p>"; $html .= "<a href=\"" . get_permalink() . "\" class=\"button\">Learn more</a>"; $html .= "</div>"; endwhile; endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.