WordPress.org

Forums

display random posts on index page (17 posts)

  1. Tri-Fusion
    Member
    Posted 8 years ago #

    I'd like to change the string below int a random posts. I'm not talking about the random from wasabi :-) do you guys think it's possible?

    " rel="bookmark" title='Permanent Link to "<?php strip_tags(the_title()); ?>"'><?php the_title(); ?>

  2. Kafkaesqui

    Posted 8 years ago #

    The string you have there is just the permalink code to a post. It would appear long before you collected your posts from the database, random or not.

    Could you provide a little more detail on what you're attempting to change? If you must post code, submit it here:

    http://wordpress.pastebin.ca

    and reply with the url to it.

  3. Tri-Fusion
    Member
    Posted 8 years ago #

    Hi Kafkaesqui,

    I want to display random posts on the front page instead of showing latest posts. The reason I do this is because I seldom update my blog

  4. Kafkaesqui

    Posted 8 years ago #

    So you want to replace The Loop with something generating a random selection of posts?

    Not too hard:

    <?php
    global $wpdb;
    $numposts = 10;

    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT $numposts");
    foreach($rand_posts as $post) :
    setup_postdata($post);
    ?>

    Template tags and whatnot go here.

    <?php endforeach; ?>

    Just change the value of $numposts to define how many posts to query for.

  5. JohnHeart
    Member
    Posted 8 years ago #

    I would like to specify categories to include and exclude. Thanks.

  6. JohnHeart
    Member
    Posted 8 years ago #

    The code above also shows pages. How can I exclude it? Thanks.

  7. bcbirk100
    Member
    Posted 8 years ago #

    use this loop to select a random post from a specific category.. remember to change the number of posts you want an dhte category name.

    <?php
    global $wpdb;
    $numposts = 1;
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->post2cat,$wpdb->posts,$wpdb->categories WHERE
    $wpdb->post2cat.post_id = $wpdb->posts.ID and $wpdb->post2cat.category_id = $wpdb->categories.cat_ID and $wpdb->posts.post_status = 'publish' and $wpdb->categories.cat_name = 'featured' ORDER BY RAND() LIMIT $numposts");
    foreach($rand_posts as $post) :
    setup_postdata($post);
    ?>

  8. Josphine
    Member
    Posted 8 years ago #

    I search exactly the same thing but... for pages (in fact for child pages of a specific parent page. There is an old plugin but it doesn't make the job correctly (all text is whithout breaks and formatting and the title isn't shown. See : http://www.aviransplace.com/index.php/get-random-page-wordpress-plugin/).

    I'm 0 for code. So if there si someone good, please help... !
    Thanks in advance :-)

  9. babyuniverse
    Member
    Posted 8 years ago #

    <?php
    global $wpdb;
    $numposts = 1;
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->post2cat,$wpdb->posts,$wpdb->categories WHERE
    $wpdb->post2cat.post_id = $wpdb->posts.ID and $wpdb->post2cat.category_id = $wpdb->categories.cat_ID and $wpdb->posts.post_status = 'publish' and $wpdb->categories.cat_name = 'featured' ORDER BY RAND() LIMIT $numposts");
    foreach($rand_posts as $post) :
    setup_postdata($post);
    ?>

    Hi Everyone, this code works find and I am now getting random posts for category "featured"

    How do I take this one step further and show random post from whichever category im currently in?

    Now if I click on another category other than featured i still get the ramdom posts from featured.

    On home page it would show randon form every category, and when in a category it would show category specific

  10. Tri-Fusion
    Member
    Posted 8 years ago #

    Thanks a lot bcbirk100, but is it possible to display multiple categories?

  11. ArchStanton
    Member
    Posted 8 years ago #

    I want to know the same thing as babyuniverse. How do you diplay random posts in a category?

  12. Tri-Fusion
    Member
    Posted 8 years ago #

    Hey, it's still showing the pages :-(

  13. ArchStanton
    Member
    Posted 8 years ago #

    To only show posts, insert this code:

    post_type = 'post'

  14. ArchStanton
    Member
    Posted 8 years ago #

    Can somebody tell me exactly where you place this code in The Loop?

    <?php
    global $wpdb;
    $numposts = 1;
    $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->post2cat,$wpdb->posts,$wpdb->categories WHERE
    $wpdb->post2cat.post_id = $wpdb->posts.ID and $wpdb->post2cat.category_id = $wpdb->categories.cat_ID and $wpdb->posts.post_status = 'publish' and $wpdb->categories.cat_name = 'featured' ORDER BY RAND() LIMIT $numposts");
    foreach($rand_posts as $post) :
    setup_postdata($post);
    ?>

    Template tags and whatnot go here.

    <?php endforeach; ?>
  15. Tri-Fusion
    Member
    Posted 8 years ago #

    thanks so much Archstaton

  16. daerchwing
    Member
    Posted 7 years ago #

    2.3 breaks this solution - can anybody please whip up some code that works with 2.3?

  17. xiscomax
    Member
    Posted 7 years ago #

    I am mui interested in this The Loop but I do not understand well where to add the code?

    Podria somebody to explain in which file is added?
    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags