Support » Fixing WordPress » Display one random post on a page

  • Resolved tudorc11

    (@tudorc11)


    Hi, I have several posts in more than 10 categories. I want to display, on a page, a random post each day.

    Could I achieve this in WordPress? Do I need to create a new page template?

    Thank you very much!

Viewing 15 replies - 1 through 15 (of 18 total)
  • 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

    Thread Starter tudorc11

    (@tudorc11)

    Could you be a bit more specific? I am not very familiar with php…

    Please see the link I gave above.

    Thread Starter tudorc11

    (@tudorc11)

    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.

    Thread Starter tudorc11

    (@tudorc11)

    Ok, thank you for your help. I’ll go ahead and try it out.

    Thread Starter tudorc11

    (@tudorc11)

    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 &raquo;'); ?>
    				<?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(); ?>
    Thread Starter tudorc11

    (@tudorc11)

    Anyone knows what to do?

    Moderator keesiemeijer

    (@keesiemeijer)

    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 -->
    Thread Starter tudorc11

    (@tudorc11)

    Nothing is being displayed 😐

    Moderator keesiemeijer

    (@keesiemeijer)

    I have your loop with the query on my server and it works. Did you publish a page with the “Main” template already?

    Thread Starter tudorc11

    (@tudorc11)

    Yes, I created a new page and chose to use the ‘Main’template instead of the default one. Am I doing something wrong? 😐

    Moderator keesiemeijer

    (@keesiemeijer)

    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.

    Thread Starter tudorc11

    (@tudorc11)

    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)?

    Moderator keesiemeijer

    (@keesiemeijer)

    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?

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Display one random post on a page’ is closed to new replies.