Wordpress blog truncate blog content to 30 words?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ringartdesign
Forum Newbie
Posts: 21
Joined: Wed Sep 10, 2008 10:11 am

Wordpress blog truncate blog content to 30 words?

Post by ringartdesign »

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;

}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Wordpress blog truncate blog content to 30 words?

Post by requinix »

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
See how $strippedDesc was truncated to 12 words? Try doing the same thing with $np->post_content and putting it into $html.
ringartdesign
Forum Newbie
Posts: 21
Joined: Wed Sep 10, 2008 10:11 am

Re: Wordpress blog truncate blog content to 30 words?

Post by ringartdesign »

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!
mnsmfp
Forum Newbie
Posts: 2
Joined: Sat Oct 25, 2008 9:29 pm

Re: Wordpress blog truncate blog content to 30 words?

Post by mnsmfp »

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
ringartdesign
Forum Newbie
Posts: 21
Joined: Wed Sep 10, 2008 10:11 am

Re: Wordpress blog truncate blog content to 30 words?

Post by ringartdesign »

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!
Post Reply