Posted By

crypticsoft on 06/22/12


Tagged

post wordpress truncate thecontent


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jmsmrgn


Limit WordPress the_content by words


 / Published in: PHP
 

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

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.

  1. //Example: apply_filters( 'the_content', wp_trim_words( get_the_content(), 15, '…' ) )
  2.  
  3. //Add this function to your functions.php file
  4. function get_news($cat_slug, $limit){
  5.  
  6.  
  7. $my_query = new WP_Query('category_name='.$cat_slug.'&posts_per_page='.$limit);
  8.  
  9. $result = "<ul class='news-feed'>";
  10.  
  11. while ($my_query->have_posts()) : $my_query->the_post();
  12. $result .= '<li class="news-item"><a href="' . get_permalink() . '" target="_blank">' . get_the_title() . '</a><p>';
  13. $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>';
  14. $result .= '</li>';
  15. endwhile;
  16. $result .= '</ul>';
  17. return $result;
  18. }
  19.  
  20. //Usage:
  21. // get company news from the 'news' category with limit of 2 posts
  22. print get_news('news', 2);

Report this snippet  

You need to login to post a comment.