• 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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter danielmhd

    (@danielmhd)

    Nobody?

    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; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Radom Link from one category’ is closed to new replies.