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 want to display a random page in the index page of my site and it's all good with that.The problem is that before the post I want to display a page depending in which category the post is posted.Website is about world countries so before the post I want to display a description of the country, some pictures and stuff.

The problem is that when I load a page, the post is no longer displayed(2pages are), so after loading a page I have to reset query.After that something bad happens and not 1 random post is displayed, but all the posts.

The code:

<?php global $more; $more = -1; //declare and set $more before The Loop ?>




<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>









<?php
if(in_category('afghanistan')) {
$recent = new WP_Query("pagename=asia/afganistanas"); while($recent->have_posts()) : $recent->the_post();?>
       <h3><?php the_title(); ?></h3>
       <?php the_content(); ?>
<?php endwhile;
}
elseif(in_category('albania')) {
$recent = new WP_Query("pagename=europe/albania"); while($recent->have_posts()) : $recent->the_post();?>
       <h3><?php the_title(); ?></h3>
       <?php the_content(); ?>
<?php endwhile;
}
else {
echo "Error!";
}
?>
<?php wp_reset_query(); ?>








<div id="loop">

<h1 class="post-title"><?php the_title(); ?></h1>


<?php the_content(); ?>



<?php endwhile;
endif; ?>
share|improve this question
1  
don't use the showposts parameter, it's depreciated in favor of posts_per_page –  m0r7if3r Mar 5 '12 at 23:05
add comment

2 Answers 2

up vote 0 down vote accepted

Assuming you have an original query that is wrapping the code you pasted, it looks like <?php wp_reset_query(); ?> is your issue, but I'm pretty sure you should be able to replace it with <?php wp_reset_postdata() ?> to get back to your original $post variable within the main query.

share|improve this answer
    
thanks a lot! It worked fine :) –  Tomas Mar 6 '12 at 6:46
add comment

Looking at your code the issue you're experiencing relates to the point in code where you call:

<?php wp_reset_query(); ?>

This is doing exactly as it says and "resetting" the query back to the default "get all posts" type of query.

To get the effect you're looking for you'll want to do the following:

<?php
$randomID = -1;
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post(); ?>
<?php
$randomID = get_the_ID();

if(in_category('afghanistan')) {
$recent = new WP_Query("pagename=asia/afganistanas"); while($recent->have_posts()) : $recent->the_post();?>
       <h3><?php the_title(); ?></h3>
       <?php the_content(); ?>
<?php endwhile;
}
elseif(in_category('albania')) {
$recent = new WP_Query("pagename=europe/albania"); while($recent->have_posts()) : $recent->the_post();?>
       <h3><?php the_title(); ?></h3>
       <?php the_content(); ?>
<?php endwhile;
}
else {
echo "Error!";
}
?>
<?php wp_reset_query(); ?>


<?php endwhile;
endif; ?>


<div id="loop">

<h1 class="post-title"><?php echo get_the_title($randomID); ?></h1>


<?php get_the_content($randomID); ?>
</div>

Try that out, that should get you at least close to what you're looking for. If you want the permalink you'll want to add that too. I haven't tested this code out, so you'll want to verify first, but it should get you close.

share|improve this answer
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.