logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Duplicates shown on category.php file due to multiple queries

I have a category of testimonials, in different sub-categories of my testimonials category. This code is on the category-#.php template file for the parent "testimonials" category, as I want to show all of the testimonials.

If a testimonial is in a particular category (45), it will have extra information to show that the testimonials in the other category don't have. When the loop returns a testimonial in category 45, I want it to query another post in a different category (which is an online profile (no I can't use the WordPress users functionality)), and get some information from it, such as the title and the attached thumbnail.

Annoyingly the second query that gets the thumbnail and the title causes posts to be displayed twice or more. I think a wp_reset_query or something somewhere should fix it, but I cannot figure out where.

Here's the code:

<?php query_posts('cat=41,42,43,44,45&paged=' . get_query_var('paged')); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $thumbid = get_post_thumbnail_id($post->ID); ?>

<?php if(in_category(45)) { ?>
<div class="post <?php the_title(); ?>">
<?php $key = 'quote_reader'; ?>
<?php $themeta = get_post_meta($post->ID, $key, TRUE); ?>

<?php $readerpicture = new WP_Query('showposts=1&cat=4&meta_key=reader_id&meta_value='. $themeta . ''); ?>
<?php if($readerpicture->have_posts()) : ?><?php while($readerpicture->have_posts()) : $readerpicture->the_post(); ?>
<?php $thumbid = get_post_thumbnail_id($post->ID); ?>
<div class="left">
<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo wp_get_attachment_url($thumbid); ?>&h=120&w=120&zc=1" alt="<?php the_title(); ?>" width="120" height="120" /></a>
<?php $key2 = 'reader_name'; ?>
<?php $readername = get_post_meta($post->ID, $key2, TRUE); ?>
<?php $permalink = get_permalink(); ?>
<?php wp_reset_query(); ?>
<?php endwhile; endif; ?>
</div>
<div class="right">
<h2>Reader: <span><a href="<?= $permalink; ?>"><?php echo $readername; ?> ID <?php echo $themeta; ?></a></span></h2>
<?php the_content(); ?>
<p>
Author: <?php echo get_post_meta($post->ID, "quote_author", $single = true); ?> |
Date: <?php echo get_post_meta($post->ID, "quote_date", $single = true); ?>
</p>
</div>
<div class="cboth"></div>
</div>

<?php } else { ?>

<div class="post <?php the_title(); ?>">
<div class="left">
<img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo wp_get_attachment_url($thumbid); ?>&h=120&w=120&zc=1" alt="<?php the_title(); ?>" width="120" height="120" />
</div>
<div class="right">
<h2><?php echo get_post_meta($post->ID, "quote_author", $single = true); ?> </h2>
<?php the_content(); ?>
</div>
<div class="cboth"></div>
</div>
<?php } ?>
<?php endwhile; ?>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
<?php endif; ?>

This question has been answered.

Dan Davies | 06/21/10 at 12:11pm Edit


(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/21/10
    12:14pm
    Rashad Aliyev says:

    Contact with me..

  • avatar
    Last edited:
    06/21/10
    1:03pm
    Bill Hunt says:

    The second query is overriding the first, so we can put the first in a temp variable until we're ready for it:

    <?php $temp_query = $wp_query; ?>
    <?php $readerpicture = new WP_Query('showposts=1&cat=4&meta_key=reader_id&meta_value='. $themeta . ''); ?>

    ...

    <?php endwhile; endif; ?>

    <?php $wp_query = $temp_query; ?>

    • 06/21/10 12:51pm

      Dan Davies says:

      That hasn't fixed it unfortunately. My code is now:

      <?php if(in_category(45)) { ?>
      <div class="post <?php the_title(); ?>">
      <?php $key = 'quote_reader'; ?>
      <?php $themeta = get_post_meta($post->ID, $key, TRUE); ?>
      <?php $temp_query = $wp_query; ?>
      <?php $readerpicture = new WP_Query('showposts=1&cat=4&meta_key=reader_id&meta_value='. $themeta . ''); ?>
      <?php if($readerpicture->have_posts()) : ?><?php while($readerpicture->have_posts()) : $readerpicture->the_post(); ?>
      <?php $thumbid = get_post_thumbnail_id($post->ID); ?>
      <div class="left">
      <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo wp_get_attachment_url($thumbid); ?>&h=120&w=120&zc=1" alt="<?php the_title(); ?>" width="120" height="120" /></a>
      <?php $key2 = 'reader_name'; ?>
      <?php $readername = get_post_meta($post->ID, $key2, TRUE); ?>
      <?php $permalink = get_permalink(); ?>
      <?php wp_reset_query(); ?>
      <?php endwhile; endif; ?>
      <?php $wp_query = $temp_query; ?>
      </div>
      <div class="right">
      <h2>Reader: <span><a href="<?= $permalink; ?>"><?php echo $readername; ?> ID <?php echo $themeta; ?></a></span></h2>
      <?php the_content(); ?>
      <p>
      Author: <?php echo get_post_meta($post->ID, "quote_author", $single = true); ?> |
      Date: <?php echo get_post_meta($post->ID, "quote_date", $single = true); ?>
      </p>
      </div>
      <div class="cboth"></div>
      </div>

      <?php } else { ?>

      <div class="post <?php the_title(); ?>">
      <div class="left">
      <img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo wp_get_attachment_url($thumbid); ?>&h=120&w=120&zc=1" alt="<?php the_title(); ?>" width="120" height="120" />
      </div>
      <div class="right">
      <h2><?php echo get_post_meta($post->ID, "quote_author", $single = true); ?> </h2>
      <?php the_content(); ?>
      </div>
      <div class="cboth"></div>
      </div>
      <?php } ?>
      <?php endwhile; ?>
      <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
      <?php endif; ?>

    • 06/21/10 12:56pm

      Bill Hunt says:

      Well, you don't need this:

      <?php wp_reset_query(); ?>


      Aside from that, it should work fine. Got a link to what you're seeing?

  • avatar
    Last edited:
    06/21/10
    1:06pm
    Oleg Butuzov says:

    tutorial #2
    http://www.smashingmagazine.com/2009/06/10/10-useful-wordpress-loop-hacks/

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.