I have a little probleme on which I really need help :( . So, I need to create in my wordpress' post something that I call "You may also like to read"! Let me tell you more about this:

For example, my wordpress post is something like this:


3-4 lines of text (like an introduction ~ theme of the article).

You may also like to read.

The rest of the post.


The "You may also like to read" contain 2 links. Im alwas generate these links manually via html code (ul, li, a tags). So it's difficult to me to always generate these links manually. I need somehow to generate these links randomely and automatically.

enter image description here

I googled for 2h and I found nothing, actually I found a plugin that can do that but it's not working (no update for more than 4 years).... I found also some php code but neither that works. So I really have no idea what I could do next :(

So can you help me?


  • You can do this with WP_Query. You say that you want to show two link randomly, presumably these are Posts and there is no specific criteria such as category or date range that need be applied?
    – David Gard
    Mar 6 '15 at 9:32


You have indicated that you wish to reuse this code, so it is better used within a function so that it can be called whenever you desire. I've made changes to my answer to reflect this below.

You can use the below code to generate a list of posts (in you case 2x random posts) together with a header. The output is formated in a way indicated by the image in your question. This code can be used anywhere within your theme -


The function that does all of the work, below, should be placed in functions.php.

If you wish, you can pass a different list title to the function, as well as a different $args array. This allows you to customise the scope of your random Posts, targeting specific categories or tags, only searching a certain date range, or only looking for Posts where some custom data value is set for example - the possibilities are endless. Check out the Class Reference for WP_Query for more information on what you can do with these args.

 * Output a list of posts under a heading
 * @param string $title The title to output in the list heading
 * @param array $args   The arguments for getting the required posts
function my_post_list($title = 'You may also like to read:', $args = array()){

    $defaults = array(  // Set some defaults for querying the Posts
        'ignore_sticky_posts'   => true,
        'posts_per_page'        => 2,
        'post_status'           => 'publish',
        'orderby'               => 'rand'
    $args = wp_parse_args($args, $defaults);    // Parse any arguments passed to this function with the defaults to create a final '$args' array
    $random = new WP_Query($args);  // Query the Posts
    if($random->have_posts()) : // Check to make sure some random Posts were returned
        echo '<h4>' . $title . '</h4>';
        echo '<ul>';
        while($random->have_posts()) : $random->the_post(); // Create a new custom Loop, and for each Post set up the postdata
            printf('<li id="random-post-%1$s">', get_the_ID());
                '<a href="%1$s" title="%2$s">%3$s</a>',
                the_title_attribute('before=Check out \'&after=\'&echo=0'),
            echo '</li>';
        wp_reset_postdata();    // Reset the postdata so that the rest of your Loop will work correctly
        echo '</ul>';
  • Thanks for your reply, but Im novice in php coding (but Im good on html, css and a little jquery). So where I will put this code ? If I put it in the "post section", wordpress will not excute it :( <br/> Can you pls be more specific ? :( <br/>thanks Mar 6 '15 at 11:08
  • This is replacing the code that you use to manually generate the random links, so it should go in the same place. Make sure that you wrap the code in PHP tags if necessary - <?php before the code and ?> after the code
    – David Gard
    Mar 6 '15 at 11:26
  • thanks alot for your reply, I succeeded. I have only one question: Everytime I put this "You May Also Like To Read:" I need to put all your code down. How I can Put this "You May Also Like To Read:" without putting your entire code everytime. Something like "id" and"class" for html which with them we costumize them with css. Thanks again for your help Mar 6 '15 at 13:09
  • See my edit, hopefullt that is what you were looking for.
    – David Gard
    Mar 6 '15 at 14:00
  • Wow Thanks you so muchhhhhh .... I'm really grateful Mar 6 '15 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.