Could I achieve this in WordPress?
Yes
Do I need to create a new page template?
Yes. One that uses a custom query.
See Creating_Your_Own_Page_Templates
Could you be a bit more specific? I am not very familiar with php…
Please see the link I gave above.
I checked it and I found this line that may be useful:
query_posts( 'orderby=rand&posts_per_page=1' );
Is this line enough to display a random post chosen from all categories or do I need to add something to it?
Thank you!
That should be enough if you only want to display a single post with no Next/Previous links.
Ok, thank you for your help. I’ll go ahead and try it out.
This the page template, it’s currently the same as the default page template. Where should I enter the query_posts() function?
<?php
/*
Template Name: Main
*/
?>
<?php get_header(); ?>
<div id="content">
<div class="entry">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<div class="head-page"></div>
<?php the_content('Read the rest of this entry »'); ?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<div class="entryclose"></div>
</div>
</div>
<?php endwhile; endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Put it before the <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
bit.
<?php query_posts( 'orderby=rand&posts_per_page=1' ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- rest of loop -->
Nothing is being displayed 😐
I have your loop with the query on my server and it works. Did you publish a page with the “Main” template already?
Yes, I created a new page and chose to use the ‘Main’template instead of the default one. Am I doing something wrong? 😐
Do you mean no posts are being displayed or do you mean you have a blank page with nothing on it when you go to visit the page.
No posts are displayed, the rest of the page is displaying fine. Could the problem be that I only have 3 posts (all in the same category)?
No this should show one random post regardless of the category. To Check:
If you leave out the query_posts
does it show the Page content of the Page you created with the Main template?