Dear Mitch,
First of all, many thanks for selflessly, tirelessly and cheerfully serving the community. You are an inspiration for us folks. Such good work has its own rewards.
Coming to my query, for some reason I can’t get more than one random post to show using the random-template (do others have the same problem?).
This is the code in the random template:
<?php else:
$related_query->query("orderby=rand&order=asc&limit=5");
$related_query->the_post();?>
<p>No related posts were found, so here's a consolation prize: <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>.</p>
<?php endif; ?>
Is it not lacking the ‘loop’ to show more than one post….?
Using other loop methods shows multiple posts though the same posts are repeated.
Anyway after much ‘copy-paste’, I stumbled on a solution using the code found here:
Just add the following after the <?php else: ?>
and before the <?php endif; ?>
command in the example-template.
<br />
<p>No related posts were found. The Latest five posts are as follows:</p>
<br /><br />
<?php
//db parameters
$db_username = '#';
$db_password = '#';
$db_database = '#';
$blog_url = 'https://localhost/experiment5/wordpress_284/index.php/'; //base folder for the blog. Make SURE there is a slash at the end
$maxchars = 135;
//connect to the database
mysql_connect(localhost, $db_username, $db_password);
@mysql_select_db($db_database) or die("Unable to select database");
//get data from database -- !IMPORTANT, the "LIMIT 5" means how many posts will appear. Change the 5 to any whole number.
$query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY id ASC LIMIT 5";
$query_result = mysql_query($query);
$num_rows = mysql_numrows($query_result);
//close database connection
mysql_close();
// html page starts after ?>
<ol>
<?php
//start a loop that starts $i at 0, and make increase until it's at the number of rows
for($i=0; $i< $num_rows; $i++){
//assign data to variables, $i is the row number, which increases with each run of the loop
$blog_date = mysql_result($query_result, $i, "post_date");
$blog_title = mysql_result($query_result, $i, "post_title");
$blog_content = mysql_result($query_result, $i, "post_content");
$blog_content = substr($blog_content, 0, $maxchars);
$blog_content = $blog_content . "...";
//$blog_permalink = mysql_result($query_result, $i, "guid"); //use this line for p=11 format.
$blog_permalink = $blog_url . mysql_result($query_result, $i, "post_name"); //combine blog url, with permalink title. Use this for title format
//format date
$blog_date = strtotime($blog_date);
$blog_date = strftime("%b %e", $blog_date);
//the following HTML content will be generated on the page as many times as the loop runs. In this case 5.
?>
<li><strong><a href="<?php echo $blog_permalink; ?>"><?php echo $blog_title; ?></a></strong>
<?php echo $blog_content; ?> <a href="<?php echo $blog_permalink; ?>">(more)</a> <br />
<br /><br /></li>
<?php
} //end the for loop
?></ol>
You can adjust the length of the excerpt with the $maxchars = 135; variable.
Works very well indeed.