• Resolved reddo

    (@reddo)


    Hi.

    Is there a way that I could set a new excerpt length before a custom wp_query and reset it after the query is done?

Viewing 4 replies - 1 through 4 (of 4 total)
  • I use this function on my site:

    function get_excerpt($count){
      $permalink = get_permalink($post->ID);
      $excerpt = get_the_content();
      $excerpt = strip_tags($excerpt);
      $excerpt = substr($excerpt, 0, $count);
      $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
      $excerpt = $excerpt.'... <a class="link" href="'.$permalink.'">read more</a>';
      return $excerpt;
    }

    That goes inside of your functions.php file.

    When you want to display the exceprt in your loop you can just do:

    <?php echo get_excerpt(250); ?>

    where 250 is the length of the excerpt.

    Evan

    Thread Starter reddo

    (@reddo)

    I’ll give it a go. This number is in characters, not words, right?

    I could wrap this in a <p> and add that read more link after it…

    I’m open to other suggestions too though if anyone has a different approach.

    Yea, 250 is the length of characters. You can do whatever you’d like to it.

    <p><?php echo get_excerpt(250); ?></p>

    The ‘read more’ link will appear after the last character you have specified. If you don’t want it there, but to just truncate and end, you can remove the ‘Read More’ link from the function above.

    Just one route, and the easiest/quickest I have found.

    Thread Starter reddo

    (@reddo)

    Tried it, working like a charm!

    Thanks for the tip!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘excerpt_length and WP_query’ is closed to new replies.