WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] Changing Excerpt length by plugin

[Resolved] Changing Excerpt length by plugin

  • Hi, I’m working on a gallery plugin which will feature a short excerpt for each post at the moment I’m setting the excerpt length like this:

    function custom_excerpt_length ( $length ) {
    	global $post;
    	if ($post->post_type == 'gallery') {
    		return 10;
    	} else {
    		return 55;
    	}
    }
    
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

    I would like to change this so it only changes the excerpt length for my gallery post type and leaves any previously defined lengths alone. Forgive me if I missed this when I searched, but could anyone point me in the right direction, thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Tara

    @t-p

    Volunteer Moderator

    see if this plugin can work for you: http://wordpress.org/plugins/character-count-excerpt/

    Sorry, I worry I may be misunderstanding something, but I don’t think that does quite what I need. The code I posted above works fine for me right now but I’m concerned that the return 55; line might interfere with other plugins or theme functions that use custom_excerpt_length. Is there a good way to adjust the excerpt length on my gallery custom post type without having to specify any other values?

    I think if you just remove the else logic, it would work fine.

    I tried that thanks, unfortunately that seems to set the excerpt length of anything that isn’t a gallery post to 0.

    Thats awfully annoying. My only advice is a hacky way. Create a variable w/ like 1000 words and then apply the filter to that content, you could then use php’s str_word_count to determine the number of words returned by the filter excerpt and then return that number for everything else.

    Sorry for the delay responding as I was away for a bit. I just wanted to come back to say thanks as your solution seems to work. For the benefit of anyone who finds this thread in the future I’ll also add that wp_trim_words looks like a good solution as well.

    Tara

    @t-p

    Volunteer Moderator

    your are welcome. 🙂

    @chrisdc1 – sorry I missed your original post. (I was away as well) Glad you found a solution. For the record, or if you still want to experiment, returning $length instead of 55 for your else condition should make your code compatible with other themes and plugins. Though you should probably set your priority to a more modest number if possible so others could more reasonably under or overshoot yours as needed.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] Changing Excerpt length by plugin’ is closed to new replies.