How to Implement a Bootstrap Carousel in WordPress Without a Plugin

How to Implement a Bootstrap Carousel in WordPress Without a Plugin

Introduction to Bootstrap Carousel in WordPress

Wordpress is the world's most popular content management system (CMS), and as such, it has a vast ecosystem of plugins and themes that help users create amazing websites. One common feature that is often desired is a Bootstrap Carousel. However, using a plugin to add a Bootstrap Carousel can sometimes lead to conflicts with other installed plugins, or may not be desirable for those who prefer to have full control over their website's functionality. In this article, we will guide you through the process of adding a Bootstrap Carousel in WordPress without the need for any plugin.

Step 1: Download Bootstrap and Register Styles and Scripts

The first step in implementing a Bootstrap Carousel in your WordPress website is to download the Bootstrap library. You can download it from the official Bootstrap website (). After downloading, you can either include these files manually or use a CDN link. Here's how to add the Bootstrap CSS and JavaScript files to your WordPress theme:

Using CDN Links:

Open your file in the theme's directory. Add the following code to include the Bootstrap CSS and JS files from the CDN:

add_action('wp_enqueue_scripts', 'enqueue_boostrap_frontend_scripts'); function enqueue_boostrap_frontend_scripts() {
    wp_enqueue_style('bootstrap-style', '@5.1.3/dist/css/bootstrap.min.css');
    wp_enqueue_script('bootstrap-script', '@5.1.3/dist/js/bootstrap.bundle.min.js', array('jquery'), '5.1.3', true);
    wp_enqueue_script('bootstrap-init', get_template_directory_uri() . '/js/bootstrap-init.js', array('bootstrap-script'), '1.1', true);
}

The bootstrap-init.js file is a custom file that we will create to initialize the Bootstrap Carousel. Save the changes and move to the next step.

Step 2: Register Custom Post Type for Slider

To create a custom carousel slider in WordPress, we need to create a custom post type. This custom post type will contain the images that will be displayed in the carousel. Here's how you can register a custom post type for the carousel slider:

Creating Custom Post Type:

add_action('init', 'slider_post_type'); function slider_post_type() {
    $labels  array(
        'name' > _x('Sliders', 'Post Type General Name', 'text_domain'),
        'singular_name' > _x('Slider', 'Post Type Singular Name', 'text_domain'),
        'menu_name' > _x('Sliders', 'Admin Menu text', 'text_domain'),
        'name_admin_bar' > _x('Slider', 'Add New on Admin Bar', 'text_domain'),
        'add_new' > _x('Add New', 'Slider', 'text_domain'),
        'add_new_item' > __('Add New Slider', 'text_domain'),
        'edit_item' > __('Edit Slider', 'text_domain'),
        'new_item' > __('New Slider', 'text_domain'),
        'view_item' > __('View Slider', 'text_domain'),
        'all_items' > __('All Sliders', 'text_domain'),
        'search_items' > __('Search Sliders', 'text_domain'),
        'parent_item_colon' > __('Parent Sliders:', 'text_domain'),
    );
    $args  array(
        'label' > __('Sliders', 'text_domain'),
        'description' > __('Carousel slider for your website', 'text_domain'),
        'labels' > $labels,
        'supports' > array('title', 'editor', 'thumbnail'),
        'taxonomies' > array(),
        'hierarchical' > false,
        'public' > true,
        'show_ui' > true,
        'show_in_menu' > true,
        'menu_position' > 5,
        'show_in_admin_bar' > true,
        'show_in_nav_menus' > true,
        'can_export' > true,
        'has_archive' > true,
        'exclude_from_search' > false,
        'publicly_queryable' > true,
        'rewrite' > array('slug' > 'slider'),
        'capability_type' > 'post',
    );
    register_post_type('slider', $args);
}

Step 3: Insert Code in Your Template to Display Slider Images

Now that we have a custom post type set up, the next step is to create a template file that will display the carousel slider. Here's an example of a template file that you can use to display the carousel slider:

Creating Carousel Template:

lt;?php
// Add Bootstrap CSS and JS files if not already enqueued
if (!wp_style_is('bootstrap-style') amp;amp; !wp_script_is('bootstrap-script')) {
    wp_enqueue_style('bootstrap-style', '@5.1.3/dist/css/bootstrap.min.css');
    wp_enqueue_script('bootstrap-script', '@5.1.3/dist/js/bootstrap.bundle.min.js', array('jquery'), '5.1.3', true);
}
// Get all sliders and display the carousel
$query  new WP_Query(array('post_type' > 'slider', 'posts_per_page' > -1));
if ($query-have_posts()) {
    echo 'lt;div classcarousel slide data-bs-ridecarousel' . PHP_EOL;
    echo 'lt;div classcarousel-inner' . PHP_EOL;
    $first  true;
    while ($query-have_posts()) {
        $query-the_post();
        $image  wp_get_attachment_image_src(get_post_thumbnail_id($post-ID), 'full');
        $item_class  $first ? 'active' : '';
        $first  false;
        echo 'lt;div classcarousel-item ' . $item_class . ' roleimg stylemin-height: 500px;' . PHP_EOL;
        echo 'lt;img src' . $image[0] . ' classd-block w-100 alt' . get_the_title() . '/' . PHP_EOL;
        echo 'lt;div classcarousel-caption d-none d-md-block' . get_the_content() . 'lt;/div' . PHP_EOL;
        echo 'lt;/div' . PHP_EOL;
    }
    echo 'lt;/div' . PHP_EOL;
    echo 'lt;a classleft carousel-control-prev href# rolebutton data-slideprev' . PHP_EOL;
    echo 'lt;span classcarousel-control-prev-icon aria-hiddentruelt;/span' . PHP_EOL;
    echo 'lt;span classsr-onlyPrevious/span' . PHP_EOL;
    echo 'lt;/a' . PHP_EOL;
    echo 'lt;a classright carousel-control-next href# rolebutton data-slidenext' . PHP_EOL;
    echo 'lt;span classcarousel-control-next-icon aria-hiddentruelt;/span' . PHP_EOL;
    echo 'lt;span classsr-onlyNext/span' . PHP_EOL;
    echo 'lt;/a' . PHP_EOL;
    echo 'lt;/div' . PHP_EOL;
    wp_reset_postdata();
}
?

Conclusion

By following these steps, you can add a Bootstrap Carousel in WordPress without the use of any plugin. This method provides full control over the functionality, and allows you to avoid conflicts with other plugins. By mastering the techniques discussed in this article, you can create visually stunning and engaging content on your WordPress website.

Keywords

Bootstrap Carousel, WordPress, Custom Post Type, jQuery, HTML