Home › Forums › Front-end Issues › ACF URL Field on Index Page WP Query
I apologize if this has been covered somewhere else. I am having a hard time finding the appropriate answer.
I am trying to take an ACF URL field from a Post and display on the Post Index page using WP Query.
Using “$variable = get_field(‘link’, $post->ID);” and “echo $variable;” is not working.
Thanks for any help.
What is the field set to return? An array or the URL?
Documentation for showing this type of field is located here https://www.advancedcustomfields.com/resources/link/ but depends on the return value setting.
Thanks. I was actually using the URL field at first, but also tried the Link field returning a URL. Both are returning empty values.
Going to need more information on the code. Is the code “Inside The Loop”? Where is code? What is the value of
Thanks for your help, John. I did a var_dump that returned NULL.
This is a WP Query outside of the Loop. Excuse my not so elegant code as I am a designer, not a developer. The value of the field is a standard URL.
<?php $args = array( 'posts_per_page' => 8 ); $variable = get_field('link', $post->ID); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); echo '<li>'; echo '<div class="img-container">'; the_post_thumbnail(); echo '</div>'; echo '<div class="news-container">'; echo '<h3>'; echo '<a href="'; the_permalink(); echo '">'; the_title(); echo '</a>'; echo '</h3>'; echo '<p class="date">'; the_date(); echo '</p>'; the_excerpt(); echo '<h4>'; echo '<a href="'; echo $variable; echo '" />'; echo 'Read More'; echo '</a></h4>'; echo '</div>'; echo '</li>'; endwhile; ?>
If you are trying to get the field from the current post in the loop then all you need to do is move that line to inside the loop
$args = array( 'posts_per_page' => 8 ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); $variable = get_field('link', $post->ID);
It actually turned out to be a naming issue. Changing the name of the field from “link” caused the field to display the URL.
Thanks for your time and help, John.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.