• hello fellow wordpress’ers.

    so, i am trying to build this tube site. but instead of having all kinds of stuff on the frontpage i am trying to keep it rather simple. i want to have

    1) one random post on the frontpage with a “give me a new random video” button or link (preferrably ajax).
    note: i thought of going to settings and only allowing 1 post but only till i realized that it affected the categories also…

    2) an advanced search widget (highest rated, most viewed, latest, specific categories etc.)

    can anyone tell me how to do this?
    i must admit that i am not the big coder. at all…

Viewing 15 replies - 1 through 15 (of 15 total)
  • Moderator t-p

    (@t-p)

    1)
    try something like this:

    <h3 class="someclass"><?php _e( 'A Random Selection of Post', 'twentyten' ); ?></h3>
    
                 <?php
                   $rand_posts = get_posts('numberposts=1&orderby=rand');
                  foreach( $rand_posts as $post ) :
                  ?>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <?php endforeach; ?>

    2).

    try searching plugins

    Thread Starter layz

    (@layz)

    it looks to me that you didnt close the h3 tag and the full post doesnt show, only the title which links to the post. Am i wrong?

    When i get home in thirty minutes i will test 🙂

    Moderator t-p

    (@t-p)

    If problem with the above code, try this:

    <h3 class="someclass"><?php _e( 'A Random Selection of Post', 'twentyten' ); ?></h3>
                 <ul>
                 <?php
                   $rand_posts = get_posts('numberposts=1&orderby=rand');
                  foreach( $rand_posts as $post ) :
                  ?>
                 <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <?php endforeach; ?>
                </ul></li>
    Thread Starter layz

    (@layz)

    so i’ve tested it now.

    the random part works but it still only shows a link to the post, not the post it self?

    Moderator t-p

    (@t-p)

    try this see if it helps;

    <h3 class="someclass"><?php _e( 'A Random Selection of Post', 'twentyten' ); ?></h3>
                 <ul>
                 <?php
                   $rand_posts = get_posts('numberposts=1&orderby=rand');
                  foreach( $rand_posts as $post ) :
                  ?>
                 <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <?php the_content(); ?>
                <?php endforeach; ?>
                </ul></li>
    Thread Starter layz

    (@layz)

    doesnt work

    Moderator t-p

    (@t-p)

    try this see if it works:

    <?php
    
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY RAND() LIMIT 1");
    
    foreach($rand_posts as $post){
    
    setup_postdata($post);
    
    the_excerpt("Read More...");
    
    }
    
    ?>
    Thread Starter layz

    (@layz)

    hmm, nothing.

    how about the loop? can you do something to that?

    Moderator t-p

    (@t-p)

    I use random post code in my sidebar where just title is displayed.

    I am running out of ideas.

    By the way – so that may someone else can help – where are you inserting the code (i.e., which template, which location…)?

    Thread Starter layz

    (@layz)

    <?php
    			/* Run the loop to output the posts.
    			 * If you want to overload this in a child theme then include a file
    			 * called loop-index.php and that will be used instead.
    			 */
    			 get_template_part( 'loop', 'index' );
    			?>

    this is what i have in index.php that, i believe, shows the posts.

    isnt there anything you can do with that?

    Thread Starter layz

    (@layz)

    i am replacing the above code and if that doesnt work i add above/beneath the code.

    thanks for your effort mate, you tried, thanks.

    i tried searching again and found this

    <ul><li><h2>A random selection of my writing</h2>
        <ul>
     <?php
     $rand_posts = get_posts('numberposts=5&orderby=rand');
     foreach( $rand_posts as $post ) :
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
        </ul>
     </li></ul>

    looks pretty much like your earlier reply. don’t understand why it’s so hard to make it show the actual post :p

    Moderator t-p

    (@t-p)

    try this (it works):

    replace this (in index.php),

    <?php
    			/* Run the loop to output the posts.
    			 * If you want to overload this in a child theme then include a file
    			 * called loop-index.php and that will be used instead.
    			 */
    			 get_template_part( 'loop', 'index' );
    			?>

    with this,

    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY RAND() LIMIT 1");
    
    foreach($rand_posts as $post){
    
    setup_postdata($post);?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php
    the_excerpt("Read More...");
    
    }
    
    			/* Run the loop to output the posts.
    			 * If you want to overload this in a child theme then include a file
    			 * called loop-index.php and that will be used instead.
    			 */
    
    			?>
    Moderator t-p

    (@t-p)

    sorry, forgot to type <?php,

    so replace with this:

    <?php
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY RAND() LIMIT 1");
    
    foreach($rand_posts as $post){
    
    setup_postdata($post);?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php
    the_excerpt("Read More...");
    
    }
    
    			/* Run the loop to output the posts.
    			 * If you want to overload this in a child theme then include a file
    			 * called loop-index.php and that will be used instead.
    			 */
    
    			?>

    This works – I tested it on my own blog site.

    Thread Starter layz

    (@layz)

    still no luck, sorry.

    Zeal

    (@zealwebdrumbeatcom)

    I used this and it worked:
    `<?php

    $rand_posts = $wpdb->get_results(“SELECT * FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_type=’post’ ORDER BY RAND() LIMIT 1”);

    foreach($rand_posts as $post){

    setup_postdata($post);
    }

    ?>
    <h1><?php the_title(); ?></h1>
    <?php the_content(“Read More…”); ?>

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘random post on frontpage’ is closed to new replies.