WordPress.org

Ready to get started?Download WordPress

Forums

Random page with content (4 posts)

  1. Dneiz
    Member
    Posted 1 year ago #

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

  2. deepbevel
    Member
    Posted 1 year ago #

    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.

  3. Dneiz
    Member
    Posted 1 year ago #

    Thanks! it is working now!

  4. deepbevel
    Member
    Posted 1 year ago #

    great!

Topic Closed

This topic has been closed to new replies.

About this Topic