0

I want to display single random post in it's entirety in the homepage.I've tried many methods, but the main issue was that it won't display the content, only heading, or display content, but only an excerpt.

Now I have this code:

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

<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>

<?php the_content(); ?>

<?php endwhile;
endif; ?>

One problem is that it doesn't display full post, only excerpt.Another problem is that it displays only heading and content and I want all the things I get in single.php.The biggest problem is that my custom code:

<?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;
}
else {
echo "Klaida!";
}
?>

which displays a page inside of post depending on the category of post doesn't word in index, but does work in single.php

Is there any method to use a copy of single.php in index and give it a random post to load every time the page is refreshed ?

1

how to show the full post: http://codex.wordpress.org/Customizing_the_Read_More#More_about_.24more

I want all the things I get in single.php

possibly copy all the code from within the loop of single.php into the loop of your random post.

my custom code ... doesn't work in index

the in_category() code might only work within the loop or needs refining - http://codex.wordpress.org/Function_Reference/in_category

  • Michael Thanks a lot.It worked except my custom code which requires to reset the loop, and if that's done it shows all the posts, but I guess it's another topic – Tomas Mar 5 '12 at 18:51

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.