Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this typical WordPress page (page.php):

<?php the_post(); ?>
<div id="rightcol">
 <div <?php post_class(); ?>>
 <?php the_content(); ?>
</div>

All works fine; have some shortcode, [gallery], in the page content.

So then I add a simple loop to the page to display random posts form a given category:

<?php query_posts('category_name=interesting_sites&posts_per_page=3&orderby=rand'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 <div <?php post_class(); ?>>
  <h1><?php the_title(); ?></h1>
  <?php the_content(); ?>
 </div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

Then, shortcode in page content fails! Why this happens? Any ideas?

share|improve this question
    
What version of WP are you using? –  Cristian Jul 9 '11 at 11:46
    
It be 3.2 - Related to the version (haven't tested)? –  tim Jul 9 '11 at 12:11
    
are you using a page template? are you trying to show the gallery before the list of posts? you might need to use two loops to do this. –  Michael Jul 9 '11 at 12:56
add comment

1 Answer

up vote 0 down vote accepted

You really shouldn't use query_posts() for anything other than the main loop of that page and use either WP_Query() or get_posts(), try this:

$my_query = new WP_Query(
    array(
        'category_name' => 'interesting_sites',
        'posts_per_page' => 3,
        'orderby' => 'rand'
    )
);
while ($my_query->have_posts()){
    $my_query->the_post();
    ?>
    <div <?php post_class(); ?>>
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
    </div>
    <?php
}
wp_reset_postdata();
share|improve this answer
    
Awesome that - I will learn the differences. Sláinte. –  tim Jul 11 '11 at 10:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.