• Resolved Nwahs

    (@nwahs)


    Site: http://semiticidol.com

    This website is going to be used to upload and then vote on videos. It is not very fair to have the same order of posts on the front page, and I have been trying to randomize the list, so every time someone comes it is in a different order. I have been screwing around with this code, but can’t get it to work:

    <?php
    $rand_posts = get_posts(‘numberposts=5&orderby=rand’);
    foreach( $rand_posts as $post ) :
    ?>
    “><?php the_title(); ?>
    <?php endforeach; ?>

    Here is part of the code on my index.php that I think is relevant:

    while ( have_posts() ) : the_post() ?>
    <div id=”post-<?php the_ID() ?>” class=”<?php sandbox_post_class() ?>”>
    <div class=”post-content”>

    <div class=”video-content” align=”center”>
    <?php
    video_blog_function(‘450′,’366’,get_the_ID());
    ?>
    </div>

Viewing 9 replies - 1 through 9 (of 9 total)
  • This may be what you want:

    $args = array(
       'posts_per_page' => 5,
       'orderby' => 'rand'
    );
    $my_query = new WP_Query($args);
    while ( $my_query->have_posts() ) : $my_query->the_post() ?>
    <div id="post-<?php the_ID() ?>" class="<?php sandbox_post_class() ?>">
    <div class="post-content"> 
    
    <div class="video-content" align="center">
    <?php
    video_blog_function('450','366',get_the_ID());
    ?>
    </div>
    Thread Starter Nwahs

    (@nwahs)

    vtxyzzy, thanks, the code seems right but I’m having trouble inserting it into the index.html. Whenever I do that I’ll either get an error message, or the code will show up on the page, above the video boxes. This is how I am trying to insert it:

    <?php while ( have_posts() ) : the_post() ?>
    
    	$args = array(
    	'posts_per_page' => 5,
    	'orderby' => 'rand'
    );
    $my_query = new WP_Query($args);
    while ( $my_query->have_posts() ) : $my_query->the_post() ?>  
    
                <div class="post-content"> 
    
                <div class="video-content" align="center">
    
    			<?php 
    
    			video_blog_function('450','366',get_the_ID());
    
    			 ?>
    
                 </div>

    Any suggestions on how to insert it so it works? Thanks

    You have 2 while loops – you only need one. Please post the full index.php at wordpress.pastebin.ca and post the link here so we can see the full program.

    Thread Starter Nwahs

    (@nwahs)

    Here is the full URL for the index.php page for the site.
    http://wordpress.pastebin.ca/1818612
    Thanks

    OK, I have updated the code as well as I can. You have a lot of code at the front of the file that really belongs in header.php: there should not be any < html >, < head >, < body >, </ html >, </ head >, </ body > tags or content in index.php. All of the opening tags should already be in header.php. The closing head tag should be in header.php and the closing body and html tags should be in footer.php.

    Until all of that is cleaned up, there is not much more I can do if any errors are still in index.php.

    Thread Starter Nwahs

    (@nwahs)

    I got read of any head, body, or HTML tags…they came with some code that I had pasted in there and I forgot to remove them. Here is the updated index.php:

    http://wordpress.pastebin.ca/1819983

    Here is a modified (hopefully corrected) version:

    http://wordpress.pastebin.ca/1820031

    Thread Starter Nwahs

    (@nwahs)

    I’m getting this error when reloading the site with the modified index.php:
    Parse error: syntax error, unexpected T_ENDWHILE in /home/mspdjcom/public_html/idol/wp-content/themes/videotheme/index.php on line 90

    Thread Starter Nwahs

    (@nwahs)

    I nailed down the right code. It seems as though I had to enter:
    <?php query_posts($query_string . '&orderby=rand') ?>

    before my wp loop. Now all my posts on my main page (index.php) are displayed in a random order upon load.

    Thanks for your help vtxyzzy, couldn’t have realized this was right w/out your help!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Change post-order on front page to random?’ is closed to new replies.