0

I'm having trouble with some PHP alongside ACF. I'm pretty unfamiliar when it comes to PHP but here's the structure. I have a ACF Select field for a category that includes Admin, Salesperson and Clerk. I am trying to render cards for each person under each category for example

Admin

Person 1 Person 3 Person 5

Salesperson

Person 2 Person 6

Clerk

Person 7

Here's the code I have:

$members_query = new WP_Query( array(
    'post_type' => 'team-member',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'menu_order',
    'order' => 'asc'
) );


// abort if no members to show
if ( ! $members_query->have_posts() ) return;

// split members into two columns 
$members_all = $members_query->posts;
$members_list = array();
foreach ($members_query->posts as $loop_index => $member_post ) {
    $members_list[] = $member_post;
}

// use a fn to output member cards for each column 
if ( ! function_exists( 'opt_render_team_member' ) ) {
    function opt_render_team_member( $member_post, $id_prefix = '' ) {

        $name = $member_post->post_title;
        $image = get_field( 'image', $member_post->ID );
        $job_title = get_field( 'job_title', $member_post->ID );
        $bio = get_field( 'bio', $member_post->ID );
        $certifications = get_field( 'certifications', $member_post->ID );
        $category = get_field ( 'category', $member_post->ID);
        ?>

        <div class="mb-7">

            <?php if ( $image ) : ?>
                <div class="team-members__headshot">
                    <?= wp_get_attachment_image( $image['ID'], 'medium_large' ); ?>
                </div>
            <?php endif; ?>

            <?php if ( $name ) : ?>
                <h2 class="team-members__name">
                    <?= $name; ?>
                </h2>
            <?php endif; ?>

            <?php if ( $job_title ) : ?>
                <h3 class="team-members__position">
                    <?= $job_title; ?>
                </h3>
            <?php endif; ?>

            <?php if ( $bio ) : ?>
                <?= $bio; ?>
            <?php endif; ?>

            <?php if ( ! empty( $certifications ) ) : ?>
                <button 
                    class="team-members__credentials-toggle" 
                    type="button" 
                    data-toggle="collapse" 
                    data-target="#team-member-<?= $id_prefix; ?>" 
                    aria-expanded="false" 
                    aria-controls="team-member-<?= $id_prefix; ?>"
                >
                    <?= __( 'See their credentials', 'optimize' ); ?>
                </button>
                <ul class="team-members__credentials collapse" id="team-member-<?= $id_prefix; ?>">
                    <?php foreach ( $certifications as $certification ) : ?>
                        <li>
                            <?= $certification['certification']; ?>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>

        </div>
        
        <?php
    }
}

?>

<div class="team-members alignfull">
    <div class="container">

        <div class="row d-none d-lg-flex">

        <?php foreach ( $members_list as $loop_index => $member_post ) : ?>
            <?php if(get_field( 'category' ) == "admin") : ?>
                <div class="col-12 col-md-6 col-lg-4">
                    <?php opt_render_team_member( $member_post, "$loop_index" ); ?>               
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

The issue is when I add the if(get_field('category') == "admin" it shows nothing, but if I remove that if statement, I see all the members but not sorted as I would like. Any advice or ideas. Thanks!

6
  • 1
    what about? <?php if(get_field('category', $member_post->ID) === 'admin') : ?> Commented Apr 26, 2022 at 22:31
  • Does not work, I've tried with =, ==, and === and no luck at all. Commented Apr 26, 2022 at 22:40
  • I ment more in the use of the 2nd argument to get_field, typically it falls back to the current post or post loaded in a wp loop(not a foreach), does this post then have the field 'category' or do you need to pass the $member_post->ID to it?? Commented Apr 27, 2022 at 1:16
  • Oh I see, yeah, it does have the field category added via ACF. I tried what you had posted and it does work now, but I don't quite understand why if you don't mind expanding. Commented Apr 27, 2022 at 3:10
  • 1
    Docs: advancedcustomfields.com/resources/get_field 2nd argument denotes while to find attached data in posts, so I presume that if you have category in the parent post, it's possibly empty, where as in the member_post it actually has the data Commented Apr 27, 2022 at 8:07

1 Answer 1

1

For fun:

<?php
$members_query = new WP_Query( array(
    'post_type' => 'team-member',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'menu_order',
    'order' => 'asc'
) );


// abort if no members to show
if ( ! $members_query->have_posts() ) return;

// split members into two columns
$members_all = $members_query->posts;
$members_list = array();
foreach ($members_query->posts as $loop_index => $member_post ) {
    $members_list[] = $member_post;
}

class TeamMemberCard
{
    public function __construct(private $memberPost = null, private $idPrefix = null){}
    
    private function hasMemberProperty(string $property): bool
    {
        return is_object($this->memberPost) && property_exists($this->memberPost, $property);
    }
    
    private function getMemberId(): ?int
    {
        if ($this->hasMemberProperty('ID')) {
            return $this->memberPost->ID;
        }
        
        return null;
    }
    
    private function getImage(string $context = 'view'): null|string|array
    {
        $image = get_field('image', $this->getMemberId());
        
        if ( ! is_array($image)) {
            return null;
        }
        
        if ($context === 'view') {
            $wpImg = wp_get_attachment_image( $image['ID'], 'medium_large' );
            return <<<RETURN
            <div class="team-members__headshot">
                {$wpImg}
            </div>
            RETURN;
        }
        
        return $image;
    }

    private function getJobTitle(string $context = 'view'): null|string
    {
        $jobTitle = get_field('job_title', $this->getMemberId());

        if (empty($jobTitle)) {
            return null;
        }

        if ($context === 'view') {
            return <<<RETURN
            <div class="team-members__position">
                {$jobTitle}
            </div>
            RETURN;
        }

        return $jobTitle;
    }

    private function getBio(): null|string
    {
        $bio = get_field('bio', $this->getMemberId());

        if (empty($bio)) {
            return null;
        }

        return $bio;
    }
    
    private function getName(string $context = 'view'): ?string
    {
        if ($this->hasMemberProperty('post_title')) {
            if ($context === 'view') {
                return <<<RETURN
                <h2 class="team-members__name">
                    {$this->getName('false')}
                </h2>
                RETURN;
            }
            
            return $this->memberPost->post_title;
        }
        
        return null;
    }
    
    private function getCertifications(string $context = 'view'): null|string|array
    {
        $certifications = get_field( 'certifications', $this->getMemberId());
        
        if (empty($certifications)) {
            return null;
        }
        
        if ($context !== 'view') {
            return $certifications;
        }
        $buttonValue = __('See their credentials', 'optimize');
        $button = <<<BUTTON
        <button
            class="team-members__credentials-toggle"
            type="button"
            data-toggle="collapse"
            data-target="#team-member-{$this->idPrefix}"
            aria-expanded="false"
            aria-controls="team-member-{$this->idPrefix}"
        >
            {$buttonValue}
        </button>
        BUTTON;
        
        $certs = <<<CERTS
        {$button}
        <ul class="team-members__credentials collapse" id="team-member-{$this->idPrefix}">
        %s
        </ul>
        CERTS;
        
        $certsLi = array();
        if (is_array($certifications)) {
            foreach($certifications as $certification) {
                if ( ! array_key_exists('certification', $certification)) {
                    continue;
                }
                $certsLi[] = <<<CERTSLI
                <li>
                    {$certification['certification']}
                </li>
                CERTSLI;
            }
        }
        
        return sprintf ($certs, implode('', $certsLi));
    }
    
    public function isAdmin(): bool
    {
        return get_field('category', $this->getMemberId()) === 'admin';
    }
    
    public function getCard(bool $echo = false): string
    {
        $card = <<<CARD
        <div class="mb-7">
            {$this->getImage()}
            {$this->getName()}
            {$this->getJobTitle()}
            {$this->getBio()}
            {$this->getCertifications()}
        </div>
        CARD;
        
        if (true === $echo) {
            echo $card;
        }
        
        return $card;
    }
}

?>

<div class="team-members alignfull">
    <div class="container">
        <div class="row d-none d-lg-flex">
            <?php
                foreach ($members_list as $loop_index => $member_post) {
                    $memberCard = new TeamMemberCard($member_post, $loop_index);
                    if ($memberCard->isAdmin()) {
                        echo <<<ECHO
                        <div class="col-12 col-md-6 col-lg-4">
                            {$memberCard->getCard()}
                        </div>
                        ECHO;
                    }
                }            
            ?>
        </div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.