Skip to content

Instantly share code, notes, and snippets.

@krogsgard
Created February 23, 2012 05:34
Show Gist options
  • Save krogsgard/1890689 to your computer and use it in GitHub Desktop.
Save krogsgard/1890689 to your computer and use it in GitHub Desktop.
Example query using wp_trim_words()
<div class="info-box">
<?php $krogsquery = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 3
)); ?>
<h3 class="info-headline">City News</h3>
<?php while($krogsquery->have_posts()) : $krogsquery->the_post(); ?>
<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<?php
$trimtitle = get_the_title();
$shorttitle = wp_trim_words( $trimtitle, $num_words = 4, $more = '… ' );
echo '<h6 class="info-title">' . '<a href="' . get_permalink() . '">' . $shorttitle . '</a></h6>';
$trimexcerpt = get_the_excerpt();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 10, $more = '… ' );
echo '<a href="' . get_permalink() . '"><p>' . $shortexcerpt . '</p></a>';
?>
</div><!-- post class -->
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
</div><!-- .info-box -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment