skip to Main Content

I have integrated the bootstrap carousel into my wordpress. Slides will be taken from pages, there will be a page called carousel and it will have subpages where post-thumbnails will be taken.
I’m still not very good at cms wordpress
someone help me please
Below is my code:

                    <div class="direction-carousel">
                        <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
                          <ol class="carousel-indicators">
                            <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
                            <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
                            <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
                          </ol>
                          <div class="carousel-inner">
                          
                              <?php 
                              $args = array( [
                                'hierarchical' => 1,
                                'child_of'     => 5,
                                'parent'       => -1,
                                'post_type'    => 'page',
                                'post_status'  => 'publish',
                            ] );
                              $pages = get_pages( $args );
                            foreach( $pages as $post ){
                                setup_postdata( $post );
                                ?>
                            <div class="carousel-item active">
                                <?php the_post_thumbnail('direction-carousel', '') ?>
                              <img class="d-block w-100" src="" alt="Первый слайд">
                            </div>

                         <?php endwhile; wp_reset_postdata(); ?>
                         <?php 
                              $args = array( [
                                'hierarchical' => 1,
                                'child_of'     => 5,
                                'parent'       => -1,
                                'post_type'    => 'page',
                                'post_status'  => 'publish',
                            ] );
                              $pages = get_pages( $args );
                            foreach( $pages as $post ){
                                setup_postdata( $post );
                                ?>

                            <div class="carousel-item">
                                <?php the_post_thumbnail('direction-carousel', '') ?>
                              
                            </div>


                             <?php endwhile; wp_reset_postdata(); ?>
                            </div>
                          </div>
                          <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
                            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                            <span class="sr-only">Previous</span>
                          </a>
                          <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
                            <span class="carousel-control-next-icon" aria-hidden="true"></span>
                            <span class="sr-only">Next</span>
                          </a>
                        </div>

3

Answers


  1. Chosen as BEST ANSWER

    I sort of figured out the code, but for some reason an error pops up: Parse error: syntax error, unexpected token ":"

    <?php
                        $i = 0;
                        $pages = get_pages([
                          'hierarchical' => 1,
                          'child_of'     => 5,
                          'parent'       => -1,
                          'post_type'    => 'page',
                          'post_status'  => 'publish',
                        ]);?>
                        <div class="direction-carousel">
                        <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
                            
                              <ol class="carousel-indicators">
                              
                              <?php for($i = 0; $i < count($pages); ++$i):
                              
                              ?>
                              <li data-target="#carouselExampleIndicators" data-slide-to="<?= $i;?>"<?=($i==0 ? ' class="active"' : '');?>></li>
                              <?php endfor; 
                              
                              $i = 0;
                              ?>
                            </ol>
                          <div class="carousel-inner">
                            <?php
                              foreach( $pages as $post ):
                                setup_postdata( $post ); ?>
                              <div class="carousel-item<?=($i == 0 : ' active' : '');?>">
                                
                                <?php the_post_thumbnail('direction-carousel', '') ?>
                              </div>
                              <?php
                                ++$i;
                              endforeach;
                              wp_reset_postdata();
                              ?>
                          </div>
                        </div>
    

  2. Try:

      <?php
                        $i = 0;
                        $pages = get_pages([
                          'hierarchical' => 1,
                          'child_of'     => 5,
                          'parent'       => -1,
                          'post_type'    => 'page',
                          'post_status'  => 'publish',
                        ]);?>
                        <div class="direction-carousel">
                        <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
                            
                              <ol class="carousel-indicators">
                              
                              <?php for($i = 0; $i < count($pages); ++$i):
                              
                              ?>
                              <li data-target="#carouselExampleIndicators" data-slide-to="<?= $i;?>"<?=($i==0 ? ' class="active"' : '');?>></li>
                              <?php endfor; 
                              
                              $i = 0;
                              ?>
                            </ol>
                          <div class="carousel-inner">
                            <?php
                              foreach( $pages as $post ):
                                setup_postdata( $post ); ?>
                              <div class="carousel-item<?=($i == 0 ? ' active' : '');?>">
                                
                                <?php the_post_thumbnail('direction-carousel', '') ?>
                              </div>
                              <?php
                                ++$i;
                              endforeach;
                              wp_reset_postdata();
                              ?>
                          </div>
                        </div>
                        
    
    Login or Signup to reply.
  3. Here are the errors it gives in the console

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search