Question

I have a wordpress site I am creating. on the post pages, I have text widget with PHP allowed, where I have a custom loop:

<?php  $my_query2 = new WP_Query("showposts=1&cat=9,10,11,18,19&orderby=rand"); ?>
        <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
         <a href="<?php the_permalink() ?>">
         <div class="home-widget-thumb">
               <?php 
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('home-thumb');
} 

?>
         </div>
            <h2><?php the_title(); ?></h2></a>

            <div class="body">

                <?php echo get_the_excerpt(); ?>

            </div><!--body-->



             </br>
            <span class="more-link">
            <a href="<?php the_permalink() ?>">[more]</a>
            </span>

            <?php endwhile; ?>
            <?php else : ?>
            <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
            <?php endif; ?>

For some reason, whatever blog post you are on is the same as in the loop I created. See an example here: http://counselingandtraining.com/play-therapy/

The loop for the post is not modified.

Can anyone tell me why this is happening?

Let me know if I can provide further info.

Thanks in advance for your time. Chris

Was it helpful?

Solution

It looks to me like the problem is that you are still using the base query.

You can change this by adding your variable $my_query2 in the code like this:

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

This will make all the functions like the_title(), the_content(), etc work as intended since they will be set to $my_query2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top