How to Display a List of Random Pages in WordPress

Home » Snippets » How to Display a List of Random Pages in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking for a way to add some excitement to your WordPress website? What if you could display a list of random pages for your visitors to explore? It’s a great way to keep your content fresh and engage your audience with new and interesting articles. In this article, we’ll show you how to easily display a list of random pages on your WordPress site, allowing your visitors to discover hidden gems and dive deeper into your content. Let’s get started!

					function wpturbo_display_random_pages( $post_type ) {
    $args = array(
        'post_type' => $post_type,
        'orderby' => 'rand',
        'posts_per_page' => 5
    );
    
    $query = new WP_Query( $args );
    
    if ( $query->have_posts() ) {
        echo "<ul>";
        while ( $query->have_posts() ) {
            $query->the_post();
            echo "<li><a href='" . get_permalink() . "'>" . get_the_title() . "</a></li>";
        }
        echo "</ul>";
        wp_reset_postdata();
    } else {
        echo "No pages found";
    }
}

add_shortcode( 'wpturbo_random_pages', 'wpturbo_display_random_pages' );
				

The code snippet provided allows you to display a list of random pages on your WordPress website.

First, we define a function called wpturbo_display_random_pages which takes a parameter $post_type specifying the type of posts to retrieve. In our case, we want to display random pages, so we pass in the value 'page' as the $post_type parameter.

Inside the function, we define an array called $args which specifies the parameters for the query. Here, we set the post_type parameter to the value of $post_type, which will be 'page' in our case. We also set the orderby parameter to 'rand', which orders the results randomly, and the posts_per_page parameter to 5 to limit the number of results to 5. You can modify these parameters based on your requirements.

Next, we create a new instance of the WP_Query class, passing in the $args array. This will execute the query and store the results in the $query variable.

We then check if the query returned any posts using the have_posts() method. If there are posts, we start an <ul> element to create an unordered list. Inside the loop, we use the the_post() function to set up the post data for each iteration. We then use the get_permalink() and get_the_title() functions to retrieve and echo the permalink and title of each page, respectively. We wrap them in <li> and <a> tags to create a clickable link.

After the loop ends, we close the <ul> element. We then use the wp_reset_postdata() function to restore the global $post variable after the loop finishes.

If the query did not return any posts, we output the message "No pages found".

Finally, we use the add_shortcode() function to register a shortcode called [wpturbo_random_pages] which will invoke the wpturbo_display_random_pages function and display the list of random pages wherever the shortcode is used on your WordPress site.

Register an account to save your snippets or go Pro to get more features.