1

I have a page set up with custom fields using Advanced Custom Fields. One of the fields is a text field for specifying the text for a read more button. Within the page template I have a post query to pull in a post and I would like to use the ACF field for the "Read More" text within this loop. However, because the ACF field is associated with the page, it doesn't work when I am trying to call it from within the post loop:

<?php query_posts('showposts=1&post_type=post&cat=1'); if ( have_posts() ): ?>
    <?php while ( have_posts() ) : the_post(); ?>
      <h4><?php the_title(); ?></h4>
      <p><?php the_excerpt(); ?></p>
  </div>
  <a class="btn" href="<?php the_permalink(); ?>">
    <!-- This is the field associated with the page -->
    <?php the_field('readmore_label'); ?>
  </a>
</div>
<?php endwhile; wp_reset_query(); endif; ?>

Is it possible to use the page custom field within the post loop in the page's template?

2
  • 3
    Pass the Page ID in your call to the_field. Since it's not a normal Loop ACF doesn't know which post or page you are asking for without explicitly telling it.
    – WebElaine
    May 24, 2017 at 14:40
  • @WebElaine thank you, that worked great! If you would like to add your comment as an answer I will gladly accept it!
    – user13286
    May 24, 2017 at 14:58

1 Answer 1

0

Pass the Page ID in your call to the_field. Since it's not a normal Loop ACF doesn't know which post or page you are asking for without explicitly telling it.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.