I have a php website that grabs wordpress blog content. I would like to know if there is a way to truncate the blog content to 30 words?
Here is my php code that works (but does not truncate content):
<?php
/* PHP to pull and publish wordpress 2 post data on an external page NO CACHE*/
define('WP_USE_THEMES', false);
require('blog/wp-blog-header.php');
$how_many=1; //How many posts do you want to show
require_once("blog/wp-config.php");
// strip out html characters to allow for truncating
$strippedDesc = strip_tags($desc);
// truncate post content to 12 words
$numwords = 12;
preg_match("/([\S]+\s*){0,$numwords}/", $strippedDesc, $regs);
$shortDesc = trim($regs[0]);
// Change this for your path to wp-config.php file
$news=$wpdb->get_results("SELECT ID,post_title,
post_content FROM $wpdb->posts
WHERE post_status= \"publish\"
ORDER BY 'ID' DESC LIMIT ".$how_many);
foreach($news as $np){
//NOTE: get other post data by selecting different
//data in the posts table...
$permalink=get_permalink($np->ID);
$html= "<p><a href=\"". $permalink .
"\">$np->post_title
$np->post_content\n</p>";
//NOTE: you can easily alter the HTML above to suit
echo $html;
}
?>
Wordpress blog truncate blog content to 30 words?
Moderator: General Moderators
-
ringartdesign
- Forum Newbie
- Posts: 21
- Joined: Wed Sep 10, 2008 10:11 am
Re: Wordpress blog truncate blog content to 30 words?
Code: Select all
// truncate post content to 12 words
$numwords = 12;
preg_match("/([\S]+\s*){0,$numwords}/", $strippedDesc, $regs);
$shortDesc = trim($regs[0]);Code: Select all
$html= "<p><a href=\"". $permalink .
"\">$np->post_title
$np->post_content\n</p>";
//NOTE: you can easily alter the HTML above to suit-
ringartdesign
- Forum Newbie
- Posts: 21
- Joined: Wed Sep 10, 2008 10:11 am
Re: Wordpress blog truncate blog content to 30 words?
Thank you for your post...but I'm pretty new to php coding, could you give me an example I could paste into my page to test?
Many thanks!
Many thanks!
Re: Wordpress blog truncate blog content to 30 words?
Here is a good tutorial site for some specific php applications that are easy to integrate into your website. This link is specific for installation of wordpress and should give you some tips.
http://www.easykiss123.com/?p=12
http://www.easykiss123.com/?p=12
-
ringartdesign
- Forum Newbie
- Posts: 21
- Joined: Wed Sep 10, 2008 10:11 am
Re: Wordpress blog truncate blog content to 30 words?
I appreciate the link, but I've already installed the blog and know my way around it. My specific question though is how to truncate the blog post that to 30 words via an external php page.
Thanks!
Thanks!