Displaying WordPress posts in a Bootstrap 5 carousel

I have two custom posts types in WordPress, ‘Members’ and ‘Projects’. Each Project should be associated with a member, which I’ve accomplished using the ‘Relationship’ field type in Advanced Custom Fields.

I have a page which lists all of the members using a WP_Query loop and within that, I’ve also set the page to loop through the 3 most recent projects for that associated member and display those beneath the member.

At the moment, if I add more than one project for a member it displays them all on top of one another but I’d like them to show in a Bootstrap 5 carousel instead. I’ve tried setting the projects to display as slides within a carousel but instead nothing gets displayed. Is this possible?

Current code is below.

<?php $loop = new WP_Query( array( 'post_type' => 'members', 'orderby' => 'date', 'order' => 'ASC', 'posts_per_page' => 20 ) ); ?>
    <?php while( $loop->have_posts() ) : $loop->the_post(); $temp_query = $wp_query; ?>
    <?php $member_url = get_field('website_url'); ?>
        <section class="member-section">
            <div class="container">
                <div class="row justify-content-center">
                    <div class="col-sm-8">
                        <h2 class="mb-5 text-center" id="<?php echo the_title(); ?>"><?php echo the_title(); ?></h2>
                            <?php echo the_content(); ?>
                            <a href="https://<?php echo $member_url; ?>" class="mt-5"><?php echo $member_url; ?></a>
                    </div>
                </div>
                <div class="row justify-content-center project-title">
                    <div class="col-sm-8">
                        <h2 class="mb-5 text-center">Highlight Projects</h2>
                    </div>
                </div>
            </div>
        </section>
        <section>
            
                    <div id="<?php the_title(); ?>Carousel" class="carousel slide" data-bs-ride="carousel">
                                <div class="carousel-inner">
                    <?php $wp_query = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'date', 'order' => 'DSC', 'posts_per_page' => 3, 'meta_query' => array(array('key' => 'member', 'value' => '"' . get_the_ID() . '"', 'compare' => 'LIKE') ) ) ); ?>
                        
                    <?php while( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
                    <?php 
                    $location = get_field('location');
                    $value = get_field('value');
                    $number_of_units = get_field('number_of_units');
                    $tenures = get_field('tenures');
                    $homes_england = get_field('homes_england');
                    $contractor = get_field('contractor');
                    $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
                            
                                    <div class="carousel-item">
                                        <div class="container-fluid">
                                        <div class="col-sm-5" style="background: url('<?php echo $thumb; ?>') no-repeat bottom center; background-size: cover;">
                                </div>
                                <div class="col-sm-7 member-projects">
                                    <h3><?php echo the_title(); ?>, <?php echo $location; ?></h3>
                                    <div class="row">
                                        <div class="col-3">
                                        <p class="mt-5">
                                            Value<br>
                                            No of units<br>
                                            Tenures<br>
                                            Homes England<br>
                                            Contractor</p>
                                        </div>
                                        <div class="col-3">
                                            <p class="mt-5">
                                            <?php echo $value; ?><br>
                                            <?php echo $number_of_units; ?><br>
                                            <?php echo $tenures; ?><br>
                                            <?php echo $homes_england; ?><br>
                                            <?php echo $contractor; ?></p>
                                        </div>
                                    </div>                            
                                </div> 
                            </div>
                        </div>
                         
                        <?php endwhile; if (isset($wp_query)) {$wp_query = $temp_query;} ?>
                        </div>  
            </div>
                </section>
    <?php endwhile; wp_reset_postdata(); ?>

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Displaying WordPress posts in a Bootstrap 5 carousel

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.