featured post same as post content

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">
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.

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

            <div class="body">

                <?php echo get_the_excerpt(); ?>


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

            <?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


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.

Tags: PHP / Wordpress

Similar questions

How to get the Page featured image, not the Post featured image
I currently have in my header.php: Which sets the background image if there is a featured image on the pages and posts. If there is none then there is a fall back where I have a default background image. My problem arises on the Blog page - The page which is set as the "Posts Page' The background image displayed on the "Posts Page" is the featured ...
Blog page featured image being overridden by blog post featured image
On my Wordpress blog page (home.php) template, I'm trying to display the featured image set for that page at the top, then the content archive grid underneath. However, instead of displaying the featured image I set for the page, the first blog post's featured image is displayed instead. Additionally (related), if I remove the first blog post's fea...
Adding short codes from a page's content on header and hiding the same from page's content
I have created a page and added the following shortcode from wp-orbit-slider I want the contents of be displayed on the header part, instead of on the content area part, which the wordpress usually does . I tried adding the the short code on header.php and it works, but the same content will be duplicated on the content area also. I need to avoid t...
Are content.php and content-single.php the same?
I understand that when using get_template_part('content', get_post_format()); it will help choosing a post format for specific page according to post format. And if there is no post format 'i.e it's standard', then it will fallback to content.php But what if I used content-single.php with some logic like this: Do I still need content.php page? is t...
2 wordpress loops showing 1 post from same post type - how to avoid showing the same post?
I got to two loops in my template files. Both querying the same thing essentially. This loop in my header: and this loop in my side bar: When these are running that same time, what would be a method of stopping the sidebar query showing the same post as the first header query? I just never ever want a duplicate to be shown.
Featured Posts Code - Repeated featured posts
I am soooo close to getting this to work but I have one outstanding issues. Any help would be greatly appreciated. The code below is what is currently in my loop. NEW CODE per CHIP This "works" minus the fact that ONLY featured posts show on the main page now.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.