Getting Duplicate Posts - 2 Loops - Help?

Discussion in 'PHP' started by jeeplaw, Jul 24, 2009.

  1. #1
    I've got a category.php file i'm playing with in creating a new theme. I can output the posts from a single specific category, but it lists the posts twice. Any ideas on how to get rid of the duplicate posts?

    ---------------
    
    <?php get_header(); ?>
    <div class="maincontent">
    <?php get_sidebar(); ?>
    
    <div class="postcontents">
    <div class="titlehold"><h3 style="font-weight:lighter" class="ptitle">Category Listing</h3>
    </div>
    <?php
    
    foreach( ( get_the_category() ) as $category ) {
    $the_query = new WP_Query('category_name=' . $category->category_nicename . '&showposts=5');
    while ($the_query->have_posts()) : $the_query->the_post();
    ?>
    			<li>
    				<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $loopcount++; ?>
    <?php $image = get_post_meta($post->ID, 'img', true); ?>
    
    <div <? if($loopcount == 1) { ?> style="margin-top:50px" <? } 
    else { ?> style="margin-top:1px" <? } ?>class="postcontent">
    
    <div class="titlehold">
    <ul class="titho">
    <li><h3 class="ptitle"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3></li>
    </ul>
    
    <ul class="titho2">
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <img class="perma" src="<?php bloginfo('template_directory'); ?>/ico/permalink.png" alt="Follow" /></a></li>
    </ul>
    </div>
    
    <div class="clear"></div>
    
    <?php if ( isset($image) && !empty($image)) { ?>
    <div class="ithumb">
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <img class="thumb" src="<?php echo $image; ?>" alt="<?php the_title(); ?>" />
    </a> 
    <ul class="comments">
    <li><?php the_time('F j, Y'); ?></li>
    <!--
    <li><a href="<?php comments_link(); ?>"><?php comments_number('No Comments Yet', '1 Comment', '% Comments'); ?></a></li> -->
    </ul>
    </div>
    <? } ?>
    
    <?php the_excerpt(); ?>
    
    </div>
    
    <div class="clear"></div>
    <div class="scissors"></div>
    
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    
    <div class="postsnavlink">
    <?php posts_nav_link(); ?>
    </div> </a>
    			</li>
    <?php endwhile; ?>
    <?php
    }
    ?>
    
    
    </div> <!-- /maincontent -->
    <?php get_footer(); ?>
    
    
    Code (markup):

     
    jeeplaw, Jul 24, 2009 IP
  2. indyonline

    indyonline Prominent Member

    Messages:
    4,626
    Likes Received:
    248
    Best Answers:
    2
    Trophy Points:
    335
    #2
    Is this extra or un opened? Highlighted in red. (scroll down in code to post nav link)
     
    indyonline, Jul 24, 2009 IP
  3. jeeplaw

    jeeplaw Well-Known Member

    Messages:
    827
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    118
    #3
    It's extra. I just took it out and it doesn't have any impact.
     
    jeeplaw, Jul 24, 2009 IP
  4. zandigo

    zandigo Greenhorn

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    What kind of duplicate posts you get? Are they in pair for every post or just duplicate some of them? Could you post your get_category(), have_posts(), and the_post() definition. I suspect the glitch could lie in those three definition. Not sure right now.

    Your code just a little bit confusing to other readers: both <? and html tag at the same time, and to the extent of a big mess. Sorry, no offending mate.

    But after solving the duplicate problem, I suggest you look into templating with php code, like xtemplate (there is another famous one, but I forget its name right now). It will separate your php and html code, makes you life much easier :D
     
    zandigo, Jul 24, 2009 IP