If I am using Elementor and want a testimonial carousel, can you write code for that?

Home » Snippets » If I am using Elementor and want a testimonial carousel, can you write code for that?
0

Created with:

Visibility: 

public

Creator: Michael Berger

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: My Testimonial Carousel
 * Plugin URI: https://example.com
 * Description: Adds a testimonial carousel using Elementor.
 * Version: 1.0.0
 * Author: WP Turbo
 * Author URI: https://wpturbo.dev
 * Text Domain: wpturbo
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

function wpturbo_testimonial_carousel_shortcode( $atts ) {
    // Extract shortcode attributes.
    $atts = shortcode_atts(
        array(
            'limit' => 5, // Number of testimonials to display.
        ),
        $atts,
        'testimonial_carousel'
    );

    // Query testimonials from the database.
    $args = array(
        'post_type'      => 'testimonial',
        'posts_per_page' => $atts['limit'],
    );
    $testimonials_query = new WP_Query( $args );

    // Output the testimonial carousel HTML.
    ob_start();
    ?>
    <div class="testimonial-carousel">
        <?php while ( $testimonials_query->have_posts() ) : $testimonials_query->the_post(); ?>
            <div class="testimonial-item">
                <div class="testimonial-content">
                    <?php the_content(); ?>
                </div>
                <div class="testimonial-author">
                    <?php the_title(); ?>
                </div>
            </div>
        <?php endwhile; ?>
    </div>
    <?php
    wp_reset_postdata();
    return ob_get_clean();
}
add_shortcode( 'testimonial_carousel', 'wpturbo_testimonial_carousel_shortcode' );
				

Explanation:

Remember to adjust the post type and HTML markup as per your requirements. Don't forget to update the plugin header with your plugin details. Also, make sure to register the testimonial post type if you haven't done so already.

Please note that this code assumes you already have Elementor installed and activated on your WordPress site.

Register an account to save your snippets or go Pro to get more features.