Finding solutions for activating items and setting data sources in Bootstrap carousel thumbnails for Wordpress

I recently attempted to incorporate a bootstrap-powered carousel with thumbnails into my product slider. I managed to get it working for the main image (large image), but ran into issues when trying to resolve problems with the thumbnail section.

Here is the HTML version that I am attempting to convert to Wordpress: http://codepen.io/RetinaInc/pen/rxksh

The top part is functioning correctly, so I tried a similar approach for the thumbnail slider component but was unable to fix it. When running the code below as-is, it results in syntax errors, so removing the thumbnail part and replacing it with HTML allows the main slider to function. However, my goal is to have the thumbnail slider work alongside it.

<?php get_header();?>
  <div class="container">
  <div class="content">
  <div class="row">
  <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="col-sm-6">
        <div id="carousel" class="carousel slide" data-ride="carousel">
            <div class="carousel-inner ">
      <?php
          $image_args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID,
   'post_mime_type' => 'image',
   'exclude' => $current_featured_image_id,
  );
          $attachments = get_posts( $image_args );

          if ( $attachments ) {
              $i = 1;
              foreach ( $attachments as $attachment ) {?>

                <div class=
                  <?php
                    echo '"';
                    echo 'item '; 
                    if ($i == 1) {
                      echo 'active';
                    }
                    $i++;
                    echo '"';
                    ?>>
                <?php
                    echo wp_get_attachment_image( $attachment->ID, 'image' );
                    ?>
                </div>
                <?php

              }

            }  


      ?>

            </div>
        </div>;

        <div class="clearfix">
        <div id="thumbcarousel" class="carousel slide" data-interval="false">
            <div class="carousel-inner">

                <?php
                $thumb_args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID,
   'post_mime_type' => 'image',
   'exclude' => $current_featured_image_id,
  );
                $thumb_attachments = get_posts( $thumb_args );
                 if ( $thumb_attachments ) {
              $i = 1;
              foreach ( $thumb_attachments as $thumbattachment ) {?>
...

<?php get_footer();?>

Answer №1

Following wingskush's suggestion, simplifying your code and adding another variable will make it work smoothly.

Once you have:

$thumb_attachments = get_posts( $thumb_args );

Proceed with:

if ( $thumb_attachments  ) {
$j = 1; $count = 0;

After looping through each attachment in the array:

foreach ( $thumb_attachments as $thumbattachment ) {?>

Implement the following logic:

if (($count % 4) == 0) {                             
   if ($j == 1) {
        echo '<div class="item active">';
     } else {
        echo '</div><!-- /item --><div class="item">';
     }
     $j++;
  }

I tested this approach, and it worked perfectly for me.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

PHP not receiving data from jQuery Ajax (POST) request

Below is an Ajax function that sends data from a page to the same page for interpretation by PHP. When using Firebug, it is observed that the data is being sent, but not received by the PHP page. However, if we switch to a $.get function and retrieve the ...

How to Align <ul> to the Right in Bootstrap 4

Looking to align a list to the right in the header section, here is the code snippet: <div class="collapse navbar-collapse" id="app-header-nav"> <ul class="navbar-nav mr-auto mt-2 mt-lg-0"> <li class="nav-item"> <a class=" ...

What prevents Django websites from being embedded within another HTML (iframe)?

I attempted to include a Django form within another HTML page, but it's not functioning correctly. I've tried on several of my other Django sites with no success. I even tested it on other websites as well. Is there a restriction on using Django ...

What is the best way to position an SVG background image at the bottom of a container?

I am facing an issue with using an SVG as a background-image on a pseudo element. The image is not as tall as the container, so I would like to position it at the bottom of the container while having the background color fill up the top portion to create a ...

Using JQuery to extract information from a JSON file

Here is the code I am working on, where I pass input username and password values. The function I have written checks if the input matches the data in a JSON file. If there is a match, an alert saying "login correct" will be displayed, otherwise it will di ...

Modifying attributes for individual components in Vue.js classes

Recently, I integrated a reusable component called "LightBox" into my website, which displays images in higher resolution. The LightBox functionality is linked to each element having a thumbnail. However, I encountered an issue. There are multiple elements ...

In JavaScript, when you update the property of a nested object within an array, the changes will also be applied to the same property for

Encountered an interesting issue in my code where updating a single property of a nested object within an array of objects results in all similar objects having the same property updated. Snippet of the Code: let financials = { qr: { controlData: [ ...

The email message content is not displaying correctly

I'm in the process of merging multiple HTML tables into a single $message, which will then be passed to the email body as shown below. // Sending the email if(smtp_mail($To,$cc, $Subject, $message, $headers)) { echo "Email Sent"; } else { echo "An ...

CakePHP allows developers to easily include images in their links using the `$this->Html->link` method. However, instead of generating HTML code for the image, it outputs ASCII characters

I can't seem to find the solution in the CakePHP 2.3.0 manual. I am trying to use $this->Html->link with $this->Html->image to create a clickable image. However, I'm encountering an issue where the ASCII rendering of quotes is being g ...

Guide to switch background image using querySelector

I am currently trying to figure out how to set the background image of a div block by using querySelector. I have attempted various methods in my test code below, but unfortunately none seem to be working. Can someone please provide assistance? <!DOC ...

Exploring the ins and outs of webpage loading speed

I am working on writing JavaScript code that includes a button to open a webpage of my choice. I now want to understand how to detect when the page I called is finished loading. Any suggestions or ideas on this topic? I apologize if my explanation was no ...

Enhancing React components with dynamic background colors for unique elements

I am encountering an issue while using a map in my code. Specifically, I want to set the background of a particular element within the map. The element I am referring to is "item .title". I aim for this element to have a background color like this: https:/ ...

Enhance your Woocommerce shopping experience by showcasing additional attributes directly on your cart page

I am using Woocommerce along with Woocommerce Bookings, and I am looking to enhance the cart by adding more information once a user has added an item there. Currently, I have the starting date of the booking and its duration displayed with labels. <dl ...

Display the output of JSON.stringify in a neatly formatted table

After sending my table data to the database using ajax, I am now trying to retrieve it by clicking on the open button. $.ajax({ type: "POST", url: "http://localhost/./Service/GetPageInfo", dataType: "json", ...

What is the best way to align an Icon in the navbar-toggler of Bootstrap 5?

When working with Angular and Bootstrap to develop a navigation bar, I encountered an issue with aligning a user icon in the navbar. The picture below shows the problem that arises when trying to center align the user icon among other links in the navbar. ...

Toggle child checkboxes when parent checkbox is clicked in angularjs

Can anyone help me figure out how to automatically select child checkboxes when the parent checkbox is clicked? In the code below, I have a parent checkbox in the table header and child checkboxes in the table body. When the parent checkbox is selected, ...

Harvest information using BeautifulSoup and Python

Exploring the world of web scraping with BeautifulSoup in Python, I am interested in extracting the package name and price of each app from the Android Play Store. For retrieving the package name, I implemented the following code : url = "https://play.go ...

Navigating with Vue Router on Internet Information Services (IIS)

I am encountering difficulties understanding why my routes are failing when I refresh my VueJS application hosted on IIS. Everything works fine during normal browsing, but once I press F5 or update view information through a button, a 403 error is thrown. ...

Changing form position dynamically based on selection can be achieved by following these steps

I am creating a website that functions as a compact Content Management System, enabling users to effortlessly modify most of the site's content. Within my rails application, I have established two models: Category and Subcategory. A Category has mult ...

Is there a way to extract the key and value pair from this function?

After performing a Json object to array conversion using the following code: var ticks= {firstkey: "firstvalue", secondkey: "secondvalue", thirdkey: "thirdValue"} var arr = $.map(ticks, function(value, index) { return [[in ...