WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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

Your Answer


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.