• Resolved JakenCD

    (@vansite)


    Hi,

    I’m trying to create a slider that works with the loop in wordpress. I created a post-type so my client can add new slides by creating specific posts (title and image) in the dashboard.

    The html is the following:

    <div class="tab-content">
        <div id="marmol" class="tab-pane fade in active">
            <div class="row">
    
                <?php $loop = new WP_Query( array( 'post_type' => 'materiales', 'orderby' => 'post_id', 'order' => 'ASC' ) ); ?>
                <div class="col-sm-6">
                    <div id="carousel" class="carousel slide" data-ride="carousel">
                        <div class="carousel-inner big-image">
                            <?php while( $loop->have_posts() ) : $loop->the_post(); ?>
                            <div class="item active">
                                <img src="<?php the_post_thumbnail(); ?>">
                                <div class="leyenda">
                                    <p><?php the_title(); ?></p>
                                </div>
    
                            </div>
    
                        </div><!-- carousel-inner big-image -->
    
                    </div><!-- carousel -->
    	   <?php endwhile; ?>			
    
                </div> <!-- /col-sm-6 -->
    
                <div class="col-sm-6">
                    <p>some text</p>
                </div> <!-- /col-sm-6 -->
    
            </div> <!-- /row -->
    
        </div><!-- marmol -->
    </div><!-- tab-content -->'

    Obviously this doesn’t work. All the images are loaded instead of just loading the correct one each time

    Then I have a small “thumb carousel” so the user can click the image that is loaded in the slider. But I’ve omitted it for now in order to work just in the slider.

    Any idea how should I call the loop to make the slider work?

    Thanks in advance

Viewing 15 replies - 1 through 15 (of 19 total)
  • What does your output look like?

    Thread Starter JakenCD

    (@vansite)

    You mean how it look?

    https://postimg.org/image/jehxe6pmv/

    Thread Starter JakenCD

    (@vansite)

    I’m pretty new at php so I’m pretty lost right now. I’m aware I’m not telling the code anywhere to just display one post

    I think I am just confused by your problem

    What exactly is not working?
    You can’t rotate between images?
    Wrong images are loading?

    Thread Starter JakenCD

    (@vansite)

    Sorry, I didn’t explain correct.

    The photo I upload was what I want (it is the static version of the website)

    here what I have:
    https://postimg.org/image/59bnt429j/

    Thread Starter JakenCD

    (@vansite)

    James
    could you think in something or should I look for an answers somewhere else?
    Thanks

    Thread Starter JakenCD

    (@vansite)

    no one? help please! hahaha

    <div class="tab-content">
        <div id="marmol" class="tab-pane fade in active">
            <div class="row">
                 <div class="col-sm-6">
                    <div id="carousel" class="carousel slide" data-ride="carousel">
                        <div class="carousel-inner big-image">
                            <?php
                            $args = array(
                                'post_type' => 'materiales',
                                'orderby' => 'post_id',
                                'order' => 'ASC'
                            );
                            $posts = get_posts($args);
                            // If posts has data, continue to do the slides...
                            if ($posts) {
                                $i = 0;
                                // for each of the items in posts do the following..
                                foreach ($posts as $post) {
                                    setup_postdata($post);
                                    $active = ($i == 0) ? 'active' : '' ;
                                    ?>
                                    <div class="item <?php echo $active; ?>">
                                        <img src="<?php the_post_thumbnail('large'); ?>">
                                        <div class="leyenda">
                                            <p><?php the_title(); ?></p>
                                        </div>
                                    </div>
                                    <?php
                                    $i++;
                                    wp_reset_postdata();
                                }
                            }
                            ?>
    
                        </div><!-- carousel-inner big-image -->
    
                    </div><!-- carousel -->
           <?php endwhile; ?>           
    
                </div> <!-- /col-sm-6 -->
    
                <div class="col-sm-6">
                    <p>some text</p>
                </div> <!-- /col-sm-6 -->
    
            </div> <!-- /row -->
    
        </div><!-- marmol -->
    </div><!-- tab-content -->

    Try the above.. You are setting active on all the slides. This version will take the first item in the array and use that as the active slide. THIS IS UNTESTED.

    Thread Starter JakenCD

    (@vansite)

    hey tex0gen,

    It didn’t work at the beginning because of the endwhile almost at the end. Then I saw that you probably had forgotten to remove that piece of code. Without it, it works.

    THANKS SO MUCH!!!

    It is not perfect though because for some reason it adds some characters. I’ve check the code and I don’t see the error.
    Let me say that these characters already appeared in my code first.

    here a screen shot
    https://postimg.org/image/lho6gwawn/

    Also I mentioned in my first message that I have a thumbcarousel so the user/visitor can choose what slide select. If you don’t mind to take a look. Don’t waste your time if it will take you too much, but if you see a fast answer please help me. Otherwise I’m going to keep working on it these next days (or years, hehe)

    <div class="tab-content">
        <div id="marmol" class="tab-pane fade in active">
            <div class="row">
                 <div class="col-sm-6">
                    <div id="carousel" class="carousel slide" data-ride="carousel">
                        <div class="carousel-inner big-image">
                            <?php
                            $args = array(
                                'post_type' => 'materiales',
                                'orderby' => 'post_id',
                                'order' => 'ASC'
                            );
                            $posts = get_posts($args);
                            // If posts has data, continue to do the slides...
                            if ($posts) {
                                $i = 0;
                                // for each of the items in posts do the following..
                                foreach ($posts as $post) {
                                    setup_postdata($post);
                                    $active = ($i == 0) ? 'active' : '' ;
                                    ?>
                                    <div class="item <?php echo $active; ?>">
                                        <img src="<?php the_post_thumbnail('large'); ?>">
                                        <div class="leyenda">
                                            <p><?php the_title(); ?></p>
                                        </div>
                                    </div>
                                    <?php
                                    $i++;
                                    wp_reset_postdata();
                                }
                            }
                            ?>
    
                        </div><!-- carousel-inner big-image -->
    
                    </div><!-- carousel -->
    
    				<div class="clearfix">
    					<div id="thumbcarousel" class="carousel slide" data-interval="false">
    						<div class="carousel-inner">
    							<div class="item active">
    			                    <div data-target="#carousel" data-slide-to="0" class="thumb"><img src="assets/images/materiales/marmol/arabescato.jpg">
    				                </div>
    			                    <div data-target="#carousel" data-slide-to="1" class="thumb"><img src="assets/images/materiales/marmol/cremamarfil.jpg"></div>
    			                    <div data-target="#carousel" data-slide-to="2" class="thumb"><img src="assets/images/materiales/marmol/cremamarfilclasico.jpg"></div>
    			                    <div data-target="#carousel" data-slide-to="3" class="thumb"><img src="assets/images/materiales/marmol/daino.jpg"></div>
    			            	</div><!-- /item -->
    
    			                <div class="item">
    			                    <div data-target="#carousel" data-slide-to="4" class="thumb"><img src="assets/images/materiales/marmol/emperadorclaro.jpg"></div>
    			                    <div data-target="#carousel" data-slide-to="5" class="thumb"><img src="assets/images/materiales/marmol/marronemperador.jpg"></div>
    			                    <div data-target="#carousel" data-slide-to="6" class="thumb"><img src="assets/images/materiales/marmol/negromarquina.jpg"></div>
    			                    <div data-target="#carousel" data-slide-to="7" class="thumb"><img src="assets/images/materiales/marmol/rojoalicante.jpg"></div>
    			                </div><!-- /item -->
    
    			                <div class="item">
    								<div data-target="#carousel" data-slide-to="8" class="thumb"><img src="assets/images/materiales/marmol/travertinoamarillo.jpg"></div>
    			                    <div data-target="#carousel" data-slide-to="9" class="thumb"><img src="assets/images/materiales/marmol/travertinorojo.jpg"></div>
    			                </div><!-- /item -->
    
    						</div><!-- /carousel-inner -->
    
    						<a class="left carousel-control" href="#thumbcarousel" role="button" data-slide="prev">
    						<span class="glyphicon glyphicon-chevron-left"></span>
    						</a>
    
    						<a class="right carousel-control" href="#thumbcarousel" role="button" data-slide="next">
    						<span class="glyphicon glyphicon-chevron-right"></span>
    						</a>
    					</div> <!-- /thumbcarousel -->
    				</div><!-- /clearfix -->
    
                </div> <!-- /col-sm-6 -->
    
                <div class="col-sm-6">
                    <p>some text</p>
                </div> <!-- /col-sm-6 -->
    
            </div> <!-- /row -->
    
        </div><!-- marmol -->
    
    </div><!-- tab-content -->

    I understand that we can simplify the thumbcarousel to just one piece of code instead of having one snippet for each post. I’ve given a look but i don’t find the answers

    Have you got a live version i can go to? I can’t see any open code tags that may leave that character.

    Also, in your code you have a clearfix wrapped around a section of code. You don’t need to wrap that in a clearfix. Just use it as an empty div like so:

    <div class="clearfix"></div><!-- /clearfix -->
                    <div id="thumbcarousel" class="carousel slide" data-interval="false">
                        <div class="carousel-inner">
                            <div class="item active">
                                <div data-target="#carousel" data-slide-to="0" class="thumb"><img src="assets/images/materiales/marmol/arabescato.jpg">
                                </div>
                                <div data-target="#carousel" data-slide-to="1" class="thumb"><img src="assets/images/materiales/marmol/cremamarfil.jpg"></div>
                                <div data-target="#carousel" data-slide-to="2" class="thumb"><img src="assets/images/materiales/marmol/cremamarfilclasico.jpg"></div>
                                <div data-target="#carousel" data-slide-to="3" class="thumb"><img src="assets/images/materiales/marmol/daino.jpg"></div>
                            </div><!-- /item -->
    
                            <div class="item">
                                <div data-target="#carousel" data-slide-to="4" class="thumb"><img src="assets/images/materiales/marmol/emperadorclaro.jpg"></div>
                                <div data-target="#carousel" data-slide-to="5" class="thumb"><img src="assets/images/materiales/marmol/marronemperador.jpg"></div>
                                <div data-target="#carousel" data-slide-to="6" class="thumb"><img src="assets/images/materiales/marmol/negromarquina.jpg"></div>
                                <div data-target="#carousel" data-slide-to="7" class="thumb"><img src="assets/images/materiales/marmol/rojoalicante.jpg"></div>
                            </div><!-- /item -->
    
                            <div class="item">
                                <div data-target="#carousel" data-slide-to="8" class="thumb"><img src="assets/images/materiales/marmol/travertinoamarillo.jpg"></div>
                                <div data-target="#carousel" data-slide-to="9" class="thumb"><img src="assets/images/materiales/marmol/travertinorojo.jpg"></div>
                            </div><!-- /item -->
    
                        </div><!-- /carousel-inner -->
    
                        <a class="left carousel-control" href="#thumbcarousel" role="button" data-slide="prev">
                        <span class="glyphicon glyphicon-chevron-left"></span>
                        </a>
    
                        <a class="right carousel-control" href="#thumbcarousel" role="button" data-slide="next">
                        <span class="glyphicon glyphicon-chevron-right"></span>
                        </a>
                    </div> <!-- /thumbcarousel -->

    If you are browsing in chrome, press F12 for dev tools. Right click that character and click “inspect element”. Look in the tools in the html code and you will see where it is added from. I suspect it COULD be the left/right arrows to navigate the carousel.

    Thread Starter JakenCD

    (@vansite)

    No it is not. It comes from the img load after the php in .item.

    I already knew that but the code, as I said, is correct. It is very weird. I thought maybe it was a bug but no idea.

    Replace this:

    <img src="<?php the_post_thumbnail('large'); ?>">

    with:

    <img src="<?php the_post_thumbnail('large'); ?>" />

    Thread Starter JakenCD

    (@vansite)

    Not working. But the problem is there. Very weird though

    Thread Starter JakenCD

    (@vansite)

    now I see the / too

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘How to make a loop work for a slider’ is closed to new replies.