Home > OS >  Using an if statement within a foreach loop with ACF
Using an if statement within a foreach loop with ACF

Time:04-28

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 >

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

            <?php if ( $name ) : ?>
                <h2 >
                    <?= $name; ?>
                </h2>
            <?php endif; ?>

            <?php if ( $job_title ) : ?>
                <h3 >
                    <?= $job_title; ?>
                </h3>
            <?php endif; ?>

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

            <?php if ( ! empty( $certifications ) ) : ?>
                <button 
                     
                    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  id="team-member-<?= $id_prefix; ?>">
                    <?php foreach ( $certifications as $certification ) : ?>
                        <li>
                            <?= $certification['certification']; ?>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>

        </div>
        
        <?php
    }
}

?>

<div >
    <div >

        <div >

        <?php foreach ( $members_list as $loop_index => $member_post ) : ?>
            <?php if(get_field( 'category' ) == "admin") : ?>
                <div >
                    <?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!

CodePudding user response:

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 >
                {$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 >
                {$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 >
                    {$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
            
            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  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 >
            {$this->getImage()}
            {$this->getName()}
            {$this->getJobTitle()}
            {$this->getBio()}
            {$this->getCertifications()}
        </div>
        CARD;
        
        if (true === $echo) {
            echo $card;
        }
        
        return $card;
    }
}

?>

<div >
    <div >
        <div >
            <?php
                foreach ($members_list as $loop_index => $member_post) {
                    $memberCard = new TeamMemberCard($member_post, $loop_index);
                    if ($memberCard->isAdmin()) {
                        echo <<<ECHO
                        <div >
                            {$memberCard->getCard()}
                        </div>
                        ECHO;
                    }
                }            
            ?>
        </div>
    </div>
</div>
  • Related