Return to Snippet

Revision: 58041
at June 22, 2012 02:22 by crypticsoft


Updated Code
//Example: apply_filters( 'the_content', wp_trim_words( get_the_content(), 15, '…' ) )

//Add this function to your functions.php file
function get_news($cat_slug, $limit){


 $my_query = new WP_Query('category_name='.$cat_slug.'&posts_per_page='.$limit);

	$result = "<ul class='news-feed'>";

  	while ($my_query->have_posts()) : $my_query->the_post(); 
		$result .= '<li class="news-item"><a href="' . get_permalink() . '" target="_blank">' . get_the_title() . '</a><p>';
		$result .= apply_filters( 'the_content', wp_trim_words( get_the_content(), 15, '&hellip;' ) ) . ' <a href="' . get_permalink() . '" target="_blank" class="read_more"><strong>read more</strong> &raquo;</a>';
		$result .= '</li>';
	endwhile; 
  		$result .= '</ul>';
  	return $result;
}

//Usage:
// get company news from the 'news' category with limit of 2 posts
print get_news('news', 2);

Revision: 58040
at June 22, 2012 02:14 by crypticsoft


Initial Code
apply_filters( 'the_content', wp_trim_words( get_the_content(), 15, '&hellip;' ) )

Initial URL
http://wordpress.stackexchange.com/questions/37858/truncate-custom-post-type-content

Initial Description
I need this function all too often and figured I'd share it. Limit the_content() by word limit by using built in WordPress functions.

Initial Title
Limit WordPress the_content by words

Initial Tags
post, wordpress

Initial Language
PHP