Home Forums ACF PRO Get field within loop using loop counter


Get field within loop using loop counter

  • I have a grid displaying a custom post type on my WordPress homepage, where I also add a text block div after the second post using a loop counter.

    I wanted to display that text block using an ACF field, but the loop only worked if I hard coded the div like this:

    <div class="projects-list" id="projects">
    	<div class="project-item"><?php the_field('intro_text_1'); ?></div>
    $args = array( 'post_type' => 'project', 'posts_per_page' => 60 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    <div class="project-item"><a href="<?php the_permalink(); ?>">
       <?php the_post_thumbnail( 'homepage-thumb' ); ?>
       <div class="overlay"><div class="caption">
                            <h2><?php the_title(); ?></h2>
                                $terms = get_the_terms($post->ID, 'work_type');
                                if ($terms) {
                                    $terms = array_map(
                                        function($obj) {
                                            return $obj->name;
                                    echo '<div class="project-work-type">(' . implode(', ', $terms) . ')</div>';
    <strong><?php if( $loop->current_post == 2 ) {
    echo '<div class="project-item text-box">Pitch Design Union makes websites, books, brands, & custom lettering, some of which are shown here.</div>';
    } ?></strong>
     <?php endwhile;?>

    I was hoping to put in something like:
    <div class="project-item"><?php the_field('intro_text_1'); ?></div>

    Is there something I’m missing? Or a workaround?

  • I may be a little lost as to were you want to put the second bit of code. Are you attempting to put it where you have this:

    echo '<div class="project-item text-box">Pitch Design Union makes......

    and it is supposed to show the same text as where you have this on the second line of your code

    <div class="project-item"><?php the_field('intro_text_1'); ?></div>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Get field within loop using loop counter’ is closed to new replies.