Wordpress random thread code help.

Discussion in 'WordPress' started by dhisky, Sep 1, 2009.

  1. #1
    Hello I want to put a random title thread on my right side bar. Anyone have this code? Because my current template don't have one. Anyone here who can share it here? It will be highly appreciated!

    Thanks!
     
    dhisky, Sep 1, 2009 IP
  2. bobjones121806

    bobjones121806 Guest

    Messages:
    1,417
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Save the following code into the random-posts.php. Placed in the / wp-content/plugins /.It becomes a plug, In the management interface for activation.

    
    <?php 
    /*
    Plugin Name: Random Posts
    Plugin URI: 
    Description: 
    Version: 
    Author: 
    Author URI: 
    */
    
    function random_posts ($limit = 5, $length = 400, $before = '<li>', $after = '</li>', $show_pass_post = false, $show_excerpt_in_title = true) {
        global $wpdb, $tableposts;
        $sql = "SELECT ID, post_title, post_date, post_content FROM $tableposts WHERE post_status = 'publish' ";
    	if(!$show_pass_post) $sql .= "AND post_password ='' ";
    	$sql .= "ORDER BY RAND() LIMIT $limit";
        $posts = $wpdb->get_results($sql);
    	$output = '';
        foreach ($posts as $post) {
           $post_title = stripslashes($post->post_title);
    	$post_date = mysql2date('j.m.Y', $post->post_date);
           $permalink = get_permalink($post->ID);
    	$post_content = strip_tags($post->post_content); 
    	$post_content = stripslashes($post_content); 
    	$post_strip = substr($post_content,0,$length);
    	$post_strip = str_replace('"', '', $post_strip);
    	$output .= $before . '<a href="' . $permalink . '" rel="bookmark" title="';
    	if($show_excerpt_in_title) {
    		$output .= $post_strip . '...  ';
          	   } else  {
    		$output .= 'Permanent Link: ' . str_replace('"', '', $post_title) . '...   ';
    	}
    	$output .= $post_date . '">' . $post_title . '</a>';
    	if(!$show_excerpt_in_title) {
    		$output .= ': ' . $post_strip . '...  ';
          	   }
    	$output .= $after;
    	}
    	echo $output;
    }
    ?>
    
    PHP:
    Where in the template call <? Php random_posts ();?>

    Call mode:
    random_posts ($ limit = 5, $ length = 400, $ before = '<li>', $ after = '</ li>', $ show_pass_post = false, $ show_excerpt_in_title = true)
     
    bobjones121806, Sep 2, 2009 IP