WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] display random posts on index.php instead of latest (12 posts)

  1. rhonorginal
    Member
    Posted 1 year ago #

    Hello.
    I have the settings so only one post (which is by default the most recent) displays with index.php

    my current working code
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    How could I change to code to display a random post from any category/date?

    It just needs to be one post if that makes a difference.

    many thanks

  2. fonglh
    Member
    Posted 1 year ago #

    $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );

    http://codex.wordpress.org/Class_Reference/WP_Query

  3. rhonorginal
    Member
    Posted 1 year ago #

    i'm not sure how that fits into what i've already got:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
     <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
    <section class="post-content clearfix">
    <?php the_content(); ?>
    </section>
  4. cinghaman
    Member
    Posted 1 year ago #

    you will use that in your functions.php file

    if(is_home())
    and the code which fonglh gave

    http://codex.wordpress.org/Function_Reference/is_home

  5. rhonorginal
    Member
    Posted 1 year ago #

    is_home();
    $query = new WP_Query( array ( 'orderby' => 'rand', 'posts_per_page' => '1' ) );

    in functions.php doesn't seem to do anything?

  6. cinghaman
    Member
    Posted 1 year ago #

    i just gave you a reference of how to do it, you have to call that in a loop
    <?php

    if (is_home() ) {

    Your code page loop

    }
    ?>

  7. cinghaman
    Member
    Posted 1 year ago #

    <?php
     $posts = get_posts('orderby=rand&numberposts=5');
    foreach($posts as $post)
     {
     ?>
    	<li>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <?php the_title(); ?>
    </a>
    	</li>
    	<?php } ?>
  8. rhonorginal
    Member
    Posted 1 year ago #

    would that not just display a list of 5 random post titles?

  9. cinghaman
    Member
    Posted 1 year ago #

    yup
    you can add the_content()
    or the_excerpt()

    this was just a reference guide to put you on track

  10. alchymyth
    Forum Moderator
    Posted 1 year ago #

    @Robin
    please re-read the original question.
    your answers are going off-track.
    if you use 'references' please quote the associated Codex chapters.

    @rhonorginal

    for instance, add this:

    <?php global $query_string;
    query_posts( $query_string . '&orderby=rand' ); ?>

    before this line:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    http://codex.wordpress.org/Function_Reference/query_posts#Preserving_Existing_Query_Parameters

  11. cinghaman
    Member
    Posted 1 year ago #

    @alchmyth i did add the codex for is_home

    and than just updated the code what fonglh wrote

  12. rhonorginal
    Member
    Posted 1 year ago #

    @alchymyth many may thanks, that works like a peach, and I can understand why.

    @robin I appreciate your time and tying to help, but you also did this to me yesterday, had me going round in circles. I appreciate you are trying to help, but would rather have no reply than some random code.

    thanks all.

Topic Closed

This topic has been closed to new replies.

About this Topic