在带有 ACF 的 foreach 循环中使用 if 语句

Using an if statement within a foreach loop with ACF

我在处理一些 PHP 和 ACF 时遇到了问题。我对 PHP 很陌生,但结构如下。我有一个类别的 ACF Select 字段,其中包括管理员、销售员和文员。例如,我正在尝试为每个类别下的每个人呈现卡片

管理员

人1人3人5

销售人员

人 2 人 6

文员

第 7 个人

这是我的代码:

$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>

问题是当我添加 if(get_field('category') == "admin" 时它什么也没显示,但是如果我删除该 if 语句,我会看到所有成员,但看不到按照我的意愿排序。任何建议或想法。谢谢!

为了好玩:

<?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>