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!
<?php if(get_field('category', $member_post->ID) === 'admin') : ?>