/ Published in: PHP

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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//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, '…' ) ) . ' <a href="' . get_permalink() . '" target="_blank" class="read_more"><strong>read more</strong> »</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);
URL: http://wordpress.stackexchange.com/questions/37858/truncate-custom-post-type-content
Comments
