query in category.php repeats itself

I’m need some help on my query.

I’m building up a category page where on top a post need to show where custom field key ‘uitleg’ has value ‘ja’. Per category there is only one post with that value. I use this post as an introduction post. Below that post I need to show all other posts in the same category minus the introduction post. This part works like a charm.

Read More

But then something strange happens. If I got 4 posts in one category (1 introduction post and 3 other posts) the query below repeated itself 4 times beneath eachother. Is there a way to stop this? Must be in one of my foreach queries. But I can’t see where this is going wrong. Anyone can see where this is going wrong?

Thanks in advance!

                <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                <?php $drukproef = get_post_meta($post->ID, 'drukproef', $single = true); ?>
                <?php $drukproefimage = get_post_meta($post->ID, 'drukproefimage', $single = true); ?>
                <?php $actief = get_post_meta($post->ID, 'actief', $single = true); ?>
                <?php $uitleg = get_post_meta($post->ID, 'uitleg', $single = true); ?>
                <?php $artistnaam = get_post_meta($post->ID, 'artistnaam', $single = true); ?>
                <?php $artisturl = get_post_meta($post->ID, 'artisturl', $single = true); ?>


                <?php global $post;
                    $categories = get_the_category();
                    foreach ($categories as $category) :
                ?>

                 <?php
                 global $post;
                 $tmp_post = $post;
                 $myposts = get_posts('posts_per_page=1' . '&paged=' . get_query_var('paged') . '&meta_key=uitleg&meta_value=ja&category='. $category->term_id);
                 foreach($myposts as $post) :
                   setup_postdata($post);
                 ?>
                 <?php $thumb = get_post_meta($post->ID, 'thumb', $single = true); ?>



                <?php $drukproef = get_post_meta($post->ID, 'drukproef', $single = true); ?>
                <?php $drukproefimage = get_post_meta($post->ID, 'drukproefimage', $single = true); ?>
                <?php $actief = get_post_meta($post->ID, 'actief', $single = true); ?>
                <?php $uitleg = get_post_meta($post->ID, 'uitleg', $single = true); ?>

                <div class="titleactive">
                        <span id="drukproeftitel"><?php the_title(); ?>:</span>
                        <span id="drukproefactief"><?php echo $drukproef; ?></span> 
                        <img src="<?php bloginfo('template_directory'); ?>/images/uitleg-actief-ja.png" width="20" height="20" />
                    </div>
                    <div class="contentactive">
                        <div id="contentholder">
                            <div class="uitleg">  
                                <?php the_content(''); ?>
                            </div>
                            <div id="uitlegdivider"><img src="<?php bloginfo('template_directory'); ?>/images/uitlegdivider.png" alt="" width="26" height="171" /></div>
                            <div class="voorbeeldimage">
                                <img src="<?php echo $drukproefimage; ?>" />
                            </div>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <?php endforeach; ?>
                    <?php $post = $tmp_post; ?>


                    <div id="uitlegbanners">
                        <div class="inzendingen">Overige inzendingen</div>
                    </div>
                    <div class="clearfix"></div>

                        <?php
                        $posts = get_posts('posts_per_page=1000&category='. $category->term_id . '&paged=' . get_query_var('paged'));
                        foreach($posts as $post) :
                        $thumb = get_post_meta($post->ID, 'thumb', $single = true);
                        ?>
                    <ul>

                        <?php if (!empty($thumb)) { ?>
                            <li>
                                <?php $thumb = get_post_meta($post->ID, 'thumb', $single = true); ?>
                                <?php $artistnaam = get_post_meta($post->ID, 'artistnaam', $single = true); ?>
                                <a href="<?php the_permalink(); ?>"><img src="<?php echo $thumb; ?>" class="img" alt="<?php the_title(); ?>" width="171" height="171" /></a>
                                <div class="corners"></div>                               
                                <a href="<?php the_permalink(); ?>"> 
                                <div class="info">
                                    <h3><?php echo $artistnaam; ?></h3>
                                    <div class="excerpt"><?php the_excerpt(); ?></div>
                                    <div class="currentrating"><?php echo the_ratings_results(get_the_id()); ?></div>
                                </div>
                                </a>
                            </li>
                        <?php } ?>
                    </ul>
                        <?php endforeach; ?>
                    <?php endforeach; ?>
                    <?php wp_reset_query(); ?>
            <?php endwhile; ?>        
            <?php else : ?>
                Sorry, no posts matched your criteria.
            <?php endif; ?>

Related posts

Leave a Reply

3 comments

  1. You should take the condition outide the loop meaning above

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    

    Therewise every time the loop begins it start the check instead of doing the check then starting the loop if the condition exist.

    Its hard for to understand because i dont understand div names
    but i think only should be inside the loop

                            <div id="contentholder">
                            <div class="uitleg">  
                                <?php the_content(''); ?>
                            </div>
                            <div id="uitlegdivider"><img src="<?php bloginfo('template_directory'); ?>/images/uitlegdivider.png" alt="" width="26" height="171" /></div>
                            <div class="voorbeeldimage">
                                <img src="<?php echo $drukproefimage; ?>" />
                            </div>
                        </div>
    

    Explanation:
    the loop = if (have_posts()) : while (have_posts())……

    Anything iside that would go again and again until there are no posts fitting the condition you set before and not inside it.

  2. You should be able to use the offset arg in your get_post() loop.

    $myposts = get_posts('posts_per_page=1' . '&paged=' . get_query_var('paged') .'&meta_key=uitleg&meta_value=ja&category='. $category->term_id.' offset =>1');
    
  3. Fixed it another way

            <?php global $wp_query;
        $cat_ID = get_query_var('cat'); ?>
    
        <?php query_posts('cat=' . $cat_ID . '&meta_key=uitleg&meta_value=ja&posts_per_page=1' . '&paged=' . get_query_var('paged')); ?>
        <?php if (have_posts()) : ?>