• Resolved bibliofille

    (@bibliofille)


    I’ve got a custom post type called “Projects” displayed in a grid on my homepage. I’d like to insert three different text blocks within this grid: one before the loop starts, one after the 3rd post, one after the 6th post.

    The content for these blocks are served up via an ACF field: ‘intro_text_1’, ‘intro_text_2’, and ‘intro_text_3’.

    I got the text block before the loop to work, but I have no idea how to insert the other two.

    I’ve read that adding a counter could be helpful here, but I’m really not sure how to correctly implement it.

    I’m currently developing locally, so I don’t have a URL to share.

    Here is the code I’m using:

    <div class="projects-list" id="projects">
    	<div class="project-item"><?php the_field('intro_text_1'); ?></div>
      <?php
    $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>
                            <?php
                                $terms = get_the_terms($post->ID, 'work_type');
                                if ($terms) {
                                    $terms = array_map(
                                        function($obj) {
                                            return $obj->name;
                                        },
                                        $terms
                                    );
                                    echo '<div class="project-work-type">(' . implode(', ', $terms) . ')</div>';
                                }
                            ?>
                        </div>
      </div></div>
      </a>
     <?php endwhile;?>
    
    	</div>

    I’ve tried various ways of including a counter, but none have worked, likely because I’m not sure where to correctly place them.

    Any help would be much appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)
  • try using the build-in loop counter;

    example:

    <?php if( $loop->current_post == 2 ) {
    //something
    } ?>

    counter starts with 0 zero for the first post in the loop.

    Thread Starter bibliofille

    (@bibliofille)

    I got that to work! Thanks! Unfortunately, I couldn’t get the ACF field to work.

    I had to hard code the div like this: `<?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>’;
    } ?>`

    I was hoping to be able to plug in something like this:
    <?php the_field('intro_text_2'); ?>

    Is there a workaround for that? Or am I stuck hard coding the text in the ACF field?

    what plugin are you using for the AFC to get ‘the_field()’ code?

    you might need to ask in that plugin’s forum…

    Thread Starter bibliofille

    (@bibliofille)

    I’m using ACF Pro. I’ll try posting to their support forum as well.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Breaking loop then restarting’ is closed to new replies.