Using an ACF field from a page within a post loop?

admin2025-01-07  3

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?

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?

Share Improve this question asked May 24, 2017 at 14:27 user13286user13286 2272 gold badges12 silver badges29 bronze badges 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 Commented 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 Commented May 24, 2017 at 14:58
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252840a77.html

最新回复(0)