0

I am working on a WordPress site with staff bio pages. At the bottom of the page, I would like to include their most recent blog posts (if they have any). I have an outer loop that pulls the fields from my Advanced Custom Field form and an inner loop where I would like to use their name as an argument for the author. Right now I am pulling all the blog posts and can't seem to figure out how to use the outer loop post info (in this case I either need the $name variable or the title from the outer loop post data. Or to use the last part of the permalink? Is that possible?

<?php while ( have_posts() ) : the_post(); 
                $name = get_the_title();
                $position = get_field("position");
                $email = get_field("email");
                $image = get_field("photo");
                $size = "full";
                $bio = get_field("bio");
?>

<div id="primary" class="site-content">

    <header id="header" role="banner" class="l-hero-title bio-hero">  
    </header>

    <div id="bio-page" class="l-crew content-wrapper">

        <article
          itemid="<?php echo the_permalink(); ?>"
          itemscope
          itemtype="http://schema.org/Person"
        >
            <figure>
                <?php if ($image && isset($image['url'])) {
                    echo '<img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '">';
                } ?>
            </figure>
            <h1 class="l-crew-hero__header-name" itemprop="name">
                  <?php echo $name; ?>
            </h1>
            <h2 class="l-crew-hero__header-position" itemprop="hasOccupation">
                  <?php echo $position; ?>
            </h2>

            <div class="l-crew-contact">
                <!-- Email -->
                <a class="l-crew-contact__item email" href="mailto:<?php echo $email; ?>" itemprop="email">
                <?php echo $email; ?></a>
            </div> 

            <section id="content" class="l-crew-bio__wrapper">
                <div class="l-crew-bio">
                    <p><?php echo $bio; ?></p> 
                </div>
            </section>


        </article>   

         <!-- BLOG POSTS SECTION -->
        <section id="posts" class="l-crew__blog-posts-wrapper">
            <div class="l-crew-posts">
                <h3 class="l-crew-posts__hding">
                    Blog Posts </h3>
                <?php
               
                $auth_query = new WP_Query( array(
                    'post_author => $name',
                    'posts_per_page' => 3, 
                ) );

                if ($auth_query->have_posts()) :
                while ($auth_query->have_posts()) :
                    $auth_query->the_post(); 

                    $title = get_the_title();
                    $image = get_field("thumbnail");
                    $date = get_the_date( 'F j, Y' );
                ?>
                <figure class="blog-image c-archive-snippet__img col-md-5">
                    <?php if ($image && isset($image['url'])) {
                        echo '<img src="' . esc_url($image['url']) . '" alt="' . esc_attr($image['alt']) . '">';
                    } ?>
                </figure>
                <div class="c-archive-snippet-text-wrapper">
                    <h3 class="c-archive-snippet__title"><?php echo $title ?></h3>
                    <span class="c-archive-snippet__date"><?php echo $date ?></span>
                    <div class="read-more-link-container">
                        <a href="<?php the_permalink(); ?>" class="c-archive-snippet__readmore a-learnmore"><p>Read More</p></a>
                    </div>
                </div>
                <?php 
                    endwhile; 
                endif;
                wp_reset_postdata(); ?>
            
            </div>
        </section>   
    </div>

</div>            
<?php endwhile; 
?>

Any suggestions would be appreciated!!!

0

1 Answer 1

0

Mapping the page to a WordPress user is the best approach. An ACF User field to select the user would give you the author ID, which can then be used easily in a WP_Query instance.

1
  • Thanks so much for your suggestion. I finally figured it out. In this case the author was not necessarily a user. It was a matter of finding the correct field name for the blog post. The field name I needed was 'authors' not 'author_name' or 'author'. Jun 1, 2023 at 18:29

Your Answer

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

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