WordPress.org

Support

Support » How-To and Troubleshooting » Random page with content

Random page with content

  • Hi,

    How can i get a random page with the content on my sidebar?

    I want to have it like this:
    – Title of a random post
    – Content of that post (max 50 words)
    – Link to that post

    I have this already, which shows 5 different pages:

    <ul>
    <?php
    global $post;
    $args = array( 'post_type' => 'page', 'numberposts' => 5, 'orderby' => 'rand',);
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    Thanks!!

Viewing 3 replies - 1 through 3 (of 3 total)
  • change

    'numberposts' => 5

    to

    'numberposts' => 1

    then, you could just use

    <?php the_excerpt();?>

    after the_title.

    Then, if you want to change your excerpt outout so it’s different from default, try the Advanced Excerpt plugin. To get custom excerpt output for this only, you’ll have to use Advanced Excerpt’s template tag with your current code and specify the output you want.

    I can’t recall the manual way to do this.

    Thanks! it is working now!

    great!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Random page with content’ is closed to new replies.