Search code examples
phpwordpressadvanced-custom-fieldsgenesis

Comparing an ACF field within a WordPress loop to see if they are the same


I have a WordPress loop that is pulling an ACF field. I need to determine if the field names are the same and if so I want to wrap them in a div. I have created a custom index page, but we want to be able to style fields with the same author name as a dropdown. So I need to somehow compare if the are the same.

This is the site I am working on http://test.improveyourenglish.com/library/ So for instance I would like to wrap "Jane Austin" in a div so that I can style it as a dropdown.

Thanks so much any help is greatly appreciated.

This is the code I am currently using

add_action('genesis_loop', 'book_archive_page');
function book_archive_page() {
echo '<div class="left-side">';
echo '<p>The following titles are sorted by author surnames.</p>';
?><div class="enter"><a href="#$term->name"><?php echo $term->name; ?>
</div></a><?php
$post_type = 'book';

// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( array( 'post_type' => $post_type ) 
);

foreach( $taxonomies as $taxonomy ) :

    // Gets every "category" (term) in this taxonomy to get the 
respective posts
    $terms = get_terms( $taxonomy );

    foreach( $terms as $term ) : ?>

        <section class="category-section">

        <div class="row">
        <div class="span12">
            <a name="<?php echo $term->name; ?>"><h2 style="padding-
   top: 300px; margin-top: -300px;"><?php echo $term->name; ?></h2>
 </a>




        </div>

        <?php
        $args = array(
                'post_type' => $post_type,
                'posts_per_page' => -1,  //show all posts
                'tax_query' => array(
                    array(
                        'taxonomy' => $taxonomy,
                        'field' => 'slug',
                        'terms' => $term->slug,
                    )
                )

            );
        $posts = new WP_Query($args);

        if( $posts->have_posts() ): while( $posts->have_posts() ) : 
  $posts->the_post(); ?>

            <div class="span4">

                <article class="inner-post clearfix">



                    <div class="inner-content">

                   <a href="<?php echo get_permalink(); ?>" title="Read <?php echo get_the_title(); ?>"><div class="author-archive-text"><?php the_field('author_full_name'); ?></div><div class="title-archive-book"><?php  echo get_the_title(); ?></div></a>


                    </div>
                </article>


            </div>

        <?php endwhile; endif; ?>
        </div>
        <hr>
        </section>

    <?php endforeach;

endforeach; ?>
<?php
 }
echo '</div>';

Solution

  • If I understand what you want correctly, what you need to do is to record the "current" author name in a variable, and use it in your loop to compare it to the next author name. If it is a different author, then end the previous author's wrapper and start a new wrapper for the next author.

    $current_author = "";    // variable to store the current author 
    
    $posts = new WP_Query($args); ?>
    
    <div class="author-wrapper"> <!-- start the wrapper div for the first author -->
    
    <?php if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
    
        <?php 
        // check if we have a new author
        if (the_field('author_full_name') != $current_author) {
            // update current_author var to make the new author the current one
            $current_author = the_field('author_full_name'); 
            // close the previous author's wrapper and start a new one
            ?>
            </div>
            <div class="author-wrapper">
        <?php } ?>
    
             <div class="span4">
                <article class="inner-post clearfix">
                    <div class="inner-content">
    
                   <a href="<?php echo get_permalink(); ?>" title="Read <?php echo get_the_title(); ?>"><div class="author-archive-text"><?php the_field('author_full_name'); ?></div><div class="title-archive-book"><?php  echo get_the_title(); ?></div></a>
    
                    </div>
                </article>
    
            </div>
    <?php endwhile; endif; ?>
    
    </div> <!-- end the wrapper div for the last author -->