WordPress.org

Ready to get started?Download WordPress

Forums

Radom Link from one category (3 posts)

  1. danielmhd
    Member
    Posted 3 years ago #

    Hi! I'm using this code to get one radom link from my posts:

    <?php
    $randomPost = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY rand() LIMIT 1");
    echo '<a href="'.$randomPost.'">Radom post</a>';
    ?>

    Buy, what can i edit to only get posts from category 21?

    Tnks!

  2. danielmhd
    Member
    Posted 3 years ago #

    Nobody?

  3. ReflectionMedia
    Member
    Posted 3 years ago #

    This should do the trick.

    <?php
    $args = array( 'numberposts' => 1, 'orderby' => 'rand', 'category' =>21 );
    $rand_posts = get_posts( $args );
    foreach( $rand_posts as $post ) : ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endforeach; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.