0

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?

  • 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
0

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();
  • Awesome that - I will learn the differences. Sláinte. – tim Jul 11 '11 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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