How to add count with PHP?

Asked

Viewed 138 times

-3

I want to put an effect Carousel in a session, on the home page, where I pick up the 3 most recent posts. But for this I need the index, change as you do loop.

As it stands, the index appears 0, for all posts. IE, the first post will stick to the <slide :index="0">, the second tb and the third equal. I imagine I need to make a condition with count, for each time you do the loop and pick up 1 post, change the number of index for +1. The second post should stay <slide :index="1">, and the third with <slide :index="2">; how do I make a condition for the index number to increase +1, as the post is called in the loop?

<div id="carousel3d">
  <carousel-3d :perspective="0" :space="200" :display="5" :controls-visible="true" :controls-prev-html="'❬'" :controls-next-html="'❭'" :controls-width="30" :controls-height="60" :clickable="true" :autoplay="true" :autoplay-timeout="5000">
            <!--  Define our WP Query Parameters -->
        <?php $the_query = new WP_Query( 'posts_per_page=3' ); ?>
            <!-- Start our WP Query -->
        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
            <!-- Display the Post Title with Hyperlink -->
        ***<slide :index="0">***
            <span class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></span>
            <?php the_post_thumbnail(); ?></div> 
                <!-- Display the Post Excerpt -->
            <?php the_excerpt(__('(more…)')); ?>Veja mais</a>
        </slide> 
        <!-- Repeat the process and reset once it hits the limit -->
        <?php 
        endwhile;
        wp_reset_postdata();
        ?>

    <p style="text-align: center;">Conheça mais</p>
     </carousel-3d>
</div>

But I don’t know how to do it. Someone can help me?

  • I edited her, I know how else to explain.

  • before while $i=0; <slide :index="<?php echo $i ?>"> and before the endwhile; place $i++;

  • @Bacco, do you have any problem trying to understand the questions of users, especially beginners? The site is flooded with pending.

  • 1

    @Leocaracciolo in this case it is better to ask the users of the pending if they have difficulty understanding the basic functioning of the site. Unfortunately someone must have posted the site as a Helpdesk by mistake (I’m talking in general, not this specific question here). What scares me the most is that users who already have time here have not understood, to be quite honest. And look who has one usually has a yellow frame explaining what to do to reopen, but I don’t see many "attempts" in that direction.

  • Thank you @Leocaracciolo

1 answer

2

According to the documentation on

https://developer.wordpress.org/reference/classes/wp_query/

Wordpress itself already has a specific property to know the content of the current post:

<slide index="<?php echo $the_query->$current_post; ?>">

Or, using an updated PHP:

<slide index="<?=$the_query->$current_post?>">

If it were in another context it could be something like this:

Out of loop: $i = 0;

And inside:

<slide index="<?php echo ++$i; ?>">

Or even:

<slide index="<?php echo $i++; ?>">

if you want to use post-increment.

But as it comes to Wordpress, no longer makes so much sense, since it has the correct property for the purpose.

  • Great, with that answer, I won’t have to ask the AP if she has trouble understanding the basic workings of the site!

  • I prefer that she (and all the other users) understand how the site works, and how to express very clearly about the points that generate doubt, because it will make it easier for everyone, and helps to get answers faster (and no kicks, as usually happens when the post gets foggy). But I was referring in particular to those who have site time and still do not understand (the more people "before" in the subject, the better it goes). This edition, in particular, showed better in which part was the difficulty: https://answall.com/revisions/353615/3

  • I have difficulty expressing myself in real life, imagine here on the pc, writing. And each person who reads, interprets in a different way. Anyway, if you’ve gotten misty or you’re breaking the rules of the site, I apologize, because I’m really new here. The important thing is that it was edited, by me and other users, and it was clear enough for some good souls to understand me kkk. Thank you :)

  • Thanks for the full answer, I have not had time to test, because I am not on my pc. But thank you so much for the answer, that’s exactly what I wanted. and I just didn’t know how to do, or express myself. Vlw kk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.