How can I make an unordered list that alternates between post types?

I have a two post types in my wordpress site — a team member post type, and a project post type. I’d like to make an alternating unordered list that displays the thumbnail and title for each post type but can’t seem to wrap my head around how to do this. For clarity I want something like this as the output:

  • Project 1
  • Team Member 1
  • Project 2
  • Team Member 2
  • My code is below. Any help would be very appreciated!

            <ul class="callout">
                        <?php $args = array('showposts' => 2, 'post_type' => 'team_member', 'orderby' => 'rand' );  
                        $team_members = get_posts($args); ?>
    
                        <?php $args = array('showposts' => 2, 'post_type' => 'project');
                        $projects = get_posts($args); ?>
    
                            <?php foreach($projects as $post) : 
                                setup_postdata( $post ); ?>
                                <?php $project_img = the_post_thumbnail_url( 'thumbnail' ); ?>
                                <?php echo $project_img; ?>
                            <?php endforeach; ?>
                            <?php wp_reset_postdata(); ?>
    
                            <?php foreach($team_members as $post) :
                                setup_postdata( $post ); ?> 
                                <?php $photo = get_field('team_member_photo'); ?>
                                <?php $image_size = 'thumbnail'; ?>
                                <?php $image = $photo['sizes'][$image_size]; ?>
                                <?php $name = get_the_title(); ?>
                                <?php $name = explode(" ", $name); ?>
                                <?php $name = array_shift($name); ?>                        
                            <?php endforeach; 
                            wp_reset_postdata(); ?>
    
    
    
    
                </ul>  <!-- .callout -->
    

    Read more here: How can I make an unordered list that alternates between post types?

    Leave a Reply

    Your email address will not be published. Required fields are marked *