WordPress.org

Forums

[resolved] Spread Loop Results Over a Series of Container Classes (7 posts)

  1. Erik Ford
    Member
    Posted 4 years ago #

    Here's something I have been trying to get my head around for a couple of days. I want to use the following custom query to return the posts based on the comment count. But I want every 4 results to be wrapped inside one container class to be used with jQuery Cycle. Is this possible?

    This is the query I am using to return the results:

    <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 8");
    foreach ($result as $post) {
    setup_postdata($post);
    $postid = $post->ID;
    $title = $post->post_title;
    $commentcount = $post->comment_count;
    if ($commentcount != 0) { ?>

    What I would like to happen is the following:

    <div class="slides">
    <!-- 4 Results -->
    </div>
    
    <div class="slides">
    <!-- 4 results -->
    </div>

    I am assuming that I have to set up a counting variable but I only know enough PHP to be dangerous. Any help is very appreciated.

  2. vtxyzzy
    Member
    Posted 4 years ago #

    I think this will do what you want:

    <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 8");
    foreach ($result as $post) {
       setup_postdata($post);
       $postid = $post->ID;
       $title = $post->post_title;
       $commentcount = $post->comment_count;
       if ($commentcount != 0) {
          if ((++$post_counter % 4 == 1) {
             if ($post_counter > 1) {
                echo "</div><!-- End of slides -->\n";  // End previous grp
             }
             echo "<div class='slides'>\n";  // Start a new grp
          }
    
    ?>

    After the end of the foreach, you need to close the last group div:

    if ($post_counter) echo "</div><!-- End of slides -->\n";

  3. Erik Ford
    Member
    Posted 4 years ago #

    Thanks for helping with this. It looks like I didn't something wrong somewhere in my code. I used the additional if statement you suggested for my query:

    <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 8");
    foreach ($result as $post) {
       setup_postdata($post);
       $postid = $post->ID;
       $title = $post->post_title;
       $commentcount = $post->comment_count;
       if ($commentcount != 0) {
          if ((++$post_counter % 4 == 1) {
             if ($post_counter > 1) {
                echo "</div><!-- End of slides -->\n";  // End previous grp
             }
             echo "<div class='slides'>\n";  // Start a new grp
          }
    
    ?>

    Followed by my code that would wrap each result, with 4 results being wrap by a div.slides.

    <div class="slides">
    <-- 4 results -->

    I didn't place a closing div for slides inside of the loop because it seemed like the if statement was going to do so. Am I correct?

    At the end of the loop I have:

    <?php if ($post_counter) echo "</div><!-- End of slides -->\n"; ?>
    
    <?php } ?><!-- closing if post counter statement -->
    <?php } ?><!-- closing custom query -->

    Is this how I should set this up? Again, your help is much appreciated.

  4. vtxyzzy
    Member
    Posted 4 years ago #

    You do not need the second statement below. The if post counter statement is closed 6 lines below the if post counter statement.

    <?php if ($post_counter) echo "</div><!-- End of slides -->\n"; ?>
    
    <?php } ?><!-- closing if post counter statement -->
    <?php } ?><!-- closing custom query -->

    If the statement you commented as <!-- closing custom query --> above is the closing brace for the foreach, then you need to add a closing div after it, not before:

    <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 8");
    foreach ($result as $post) {
       setup_postdata($post);
       $postid = $post->ID;
       $title = $post->post_title;
       $commentcount = $post->comment_count;
       if ($commentcount != 0) {
          if ((++$post_counter % 4 == 1) {
             if ($post_counter > 1) {
                echo "</div><!-- End of slides -->\n";  // End previous grp
             }
             echo "<div class='slides'>\n";  // Start a new grp
          }
    
    ?>
      <!-- All the code inside the foreach loop -->
    <?php } ?><!-- closing custom query aka foreach loop -->
    </div><!-- End of slides -->
  5. Erik Ford
    Member
    Posted 4 years ago #

    Thanks for all of your help today. It's still not working for me. I get the following error:

    Parse error: syntax error, unexpected '{'

    which is referring to the bracket following if ((++$post_counter % 4 == 1)

    I don't want to waste your time so I will probably have to table this idea until I can figure out how to make it work. Thanks again.

  6. vtxyzzy
    Member
    Posted 4 years ago #

    Don't give up yet - it's just my wayward fingers this time. I had an extra opening paren in that statement. Please try this:

    <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 8");
    foreach ($result as $post) {
       setup_postdata($post);
       $postid = $post->ID;
       $title = $post->post_title;
       $commentcount = $post->comment_count;
       if ($commentcount != 0) {
          if (++$post_counter % 4 == 1) {
             if ($post_counter > 1) {
                echo "</div><!-- End of slides -->\n";  // End previous grp
             }
             echo "<div class='slides'>\n";  // Start a new grp
          }
    
    ?>
      <!-- All the code inside the foreach loop -->
    <?php } ?><!-- closing custom query aka foreach loop -->
    </div><!-- End of slides -->
  7. Erik Ford
    Member
    Posted 4 years ago #

    Thank you so much. That worked like a charm.

Topic Closed

This topic has been closed to new replies.

About this Topic