Great people of Stack, I once again require your assistance.
I currently have an external website that pulls through blog content from a sub-folder containing a WordPress installation, for example:
Website A: External Static Website
Website B: WordPress installation
I have the posts including on the homepage of Website A using the following code.
WordPress call:
<?php
//db parameters
$db_username = '###';
$db_password = '###';
$db_database = '###';
$blog_url = 'http://www.website.com/blog/';
//connect to the database
mysql_connect('###', $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 DESC LIMIT 1";
$query_result = mysql_query($query);
$num_rows = mysql_numrows($query_result);
//close database connection
mysql_close();
// html page starts after
?>
The Blog Post inclusion:
<div class="contentBox">
<?php
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_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.
?>
<div class="post"></div>
<img src="img/headers/news-from.png" /><br />
<p class="blogName"><a href="http://www.website.com/blog"><?php echo $blog_title; ?></a></p>
<p style="margin-top: -10px; margin-right: 10px;"><?php echo $blog_content;?></p>
<p>Submitted on: <span class="green"><?php echo $blog_date; ?></span></p>
<p><a href=â<?php echo $blog_permalink; ?>â>Read Full Post</a></p>
<?php
} //end the for loop
?>
</div>
This works perfectly, it pulls the necessary post and displays it, all wonderfully formatted etc. The problem I have is that I really need to limit the character count that is pulled through, as it stands, this currently echo’s the entire post, I only need to echo the first 15 characters of the post. Any ideas would be greatly appreciated.
You can limit the character output by echoing the following.
From:
To:
This may seem like an easyish solution to a complex question, but why not use a
substr
to trim down the post??You could even do this by setting a seperate variable?
e.g.
All this has done is create a new variable to display on the page which starts at character 0 of your
$blog_content
variable, and cuts off after 15 characters.One way to do this is to remove the line
and replace it with
Changing 50 to your desired length.