Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6154013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:11:22+00:00 2026-05-23T20:11:22+00:00

I’m using the following to create a slide for a slideshow out of every

  • 0

I’m using the following to create a slide for a slideshow out of every two posts.

<?php $i = 0; $slideshow_query = new WP_Query("showposts=6&cat=10"); ?>
    <div id="slideshow-posts">
    <div class="newslide">
    <?php while ($slideshow_query->have_posts()) : $slideshow_query->the_post();      $do_not_duplicate = $post->ID; $i++;  ?>
    //my content
    <?php if ($i == 2) { ?></div>  <div class="newslide"><?php $i = 0; } ?>
    <?php endwhile; ?>
</div>

Problem is it creates a blank slide at the end – how would I get it to carry out creation of a news slide every two posts (or every even post) except for the last time an even post occurs?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:11:22+00:00Added an answer on May 23, 2026 at 8:11 pm

    The operator you’re likely looking for is called modulus or modulo and returns your the remainder of a division:

    $foo = 5 % 3;           //  5 = 1*3 + a remainder of 2
    $bar = 17 % 5;          // 17 = 2*5 + a remainder of 2
    $baz = 42 % 6;          // 42 = 7*6 + a remainder of 0
    

    This operator comes in handy to identify even/odd rows in a loop when used with a divisor of 2:

    0 % 2 => 0
    1 % 2 => 1
    2 % 2 => 0
    3 % 2 => 1
    4 % 2 => 0
    …
    

    Or in your case something like this:

    <?php $i = 0; $slideshow_query = new WP_Query("showposts=6&cat=10"); ?>
    <div id="slideshow-posts">
        <div class="newslide">
            <?php while ($slideshow_query->have_posts()) : $slideshow_query->the_post(); ?>   
                //my content
                <?php if ($i % 2 == 0): /* even: */ ?>
                    </div><div class="newslide">
                <?php elseif ($i+1 == $slideshow_query->post_count): /* last: */ ?>
                    </div>
                <?php endif; ?>
        <?php $i++; /* iterate in the end */ ?>
    <?php endwhile; ?>
    </div>
    

    In order to find out of you have reached the last post, you could compare $i + 1 (since we start counting from 0) with the full amount of items returned by your query (max 6 in your example). And then close the <div> if they match.

    UPDATE

    I’ve extended the example above according to my comment. This isn’t tested, but I believe that’s how I remember it to work (the post count that is).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.