SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation PHP Multiples of 2, Show posts...not working (wordpress)

    I have been using this code to show div.example with 6 li columns inside it, each li is a post with its info inside.

    Once it gets to the 6th li it closes the div.example and starts a new one. This to enable me to divide them with a dotted line.

    However, when it gets to the 4th row it prints an empty div.example because there are no li's to go in it, i just need it to stop once it gets to 24, now its stopping at 24 but printing one final div.example...

    Really i need it to stop when there is no more li's to show, which it does however its printing this extra div.example and i dont know why.

    You can see it here the big gap at the bottom is another div.example its showing with nothing inside it!


    (only part of the whole page code as its the only bit that is not working correctly and isn't connected to anything else.)
    PHP Code:
    <?php

          $page 
    = (get_query_var('paged')) ? get_query_var('paged') : 1;
          
    $postcnt 0query_posts('cat=3,4,5,6');
          if (
    have_posts()) { ?>
             <div class="example">
          <ul class="content_cols">
             <?php
             
    while ( have_posts() ) : the_post(); $postcnt++; $data get_post_meta$post->ID'key'true );?>

                <li class="cat<?php $category get_the_category(); echo $category[0]->cat_ID;?>">

    <h3><?php the_title(); ?></h3>
    <p><?php echo $data'event_date' ]; ?></p>
    <p><?php echo $data'event_location' ]; ?></p>
    <p><?php echo $data'event_time' ]; ?></p>
    <p><a href="<?php the_permalink() ?>" title="View more details of <?php the_title(); ?>">Read More</a></p>
    <div class="clearFix"><!--x--></div>
    </li>

                  <?php if($postcnt &#37; 2 == 0) { ?>

                   <?php if($postcnt == 0) { ?>
                   </ul>
                   <div class="line"></div>
                   <div class="clearFix"><!--x--></div>
                      </div>

                      <div class="example">
                   <?php ?>
                   <ul class="content_cols">
                <?php ?>
             <?php
             
    endwhile; ?>
             </ul>
             <div class="clearFix"><!--x--></div>
             </div>
             <?php
          
    ?>
    The forum doesn't seem to be showing the first postcnt code properly, its the same as the one below just with 2 rather than 6

    Any ideas?

    Obviously very annoying as dont want that final div.example there at all as nothing is in it!!

    Many thanks for any help

  2. #2
    SitePoint Addict
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there another loop going on?

    It looks like the test "if (have_posts()) {" is where the issue might be at ... for some reason the check that determines whether have_posts = true is setting it to true for the last iteration ...

  3. #3
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No other loops that i know of.

    I'm not really sure what is going on, i pieced it together from tutorials and reading, without really understanding what does what.

    This last bit
    PHP Code:
    <div class="example">
                        <?php ?>
                        <ul class="content_cols">
                    <?php ?>
                <?php
                
    endwhile; ?>
                </ul>
                <div class="clearFix"><!--x--></div>
                </div>
                <?php
            
    ?>
    Tells the final row of li columns not to have the line in there <div class="clearFix"><!--x--></div> in it.

    However, when it gets to 6 li rows it prints the div.example, hence the big block of empty white space with nothing in it. There are only 24 posts showing however, i need that to be dynamic however many posts are showing it will do its job whether its 24 or 44 posts.

  4. #4
    SitePoint Addict
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm ... the key seems to be in finding out what determines the have_posts() return as true or false. I don't see it in the code posted ...

    if you look at the conditional, "if (have_posts()) {", whenever it passes, it adds the following HTML structure and this is what's happening on the last iteration through the posts.

    HTML Code:
    <div class="example">
        <ul class="content_cols"></ul>
        <div class="clearFix"><!--x--></div>
    </div>
    Take a look at how the method have_posts() is working. My guess is that there is something going on with it that is giving you a true on that last iteration when it should be false.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Err ok, as i said i'm a bit of a novice when it comes to this i'm not sure which bit is controlling what really...

  6. #6
    SitePoint Addict
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry ... I'm not familiar yet with the WordPress source code so I don't know either ... if you can, try doing a search for "function have_posts" on the source code and see how it works.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •