WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Changing Excerpt length by plugin (9 posts)

  1. Chrisdc1
    Member
    Posted 11 months ago #

    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.

  2. Tara
    Member
    Posted 11 months ago #

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

  3. Chrisdc1
    Member
    Posted 11 months ago #

    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?

  4. Timothy Jacobs
    Member
    Posted 11 months ago #

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

  5. Chrisdc1
    Member
    Posted 11 months ago #

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

  6. Timothy Jacobs
    Member
    Posted 11 months ago #

    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.

  7. Chrisdc1
    Member
    Posted 10 months ago #

    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.

  8. Tara
    Member
    Posted 10 months ago #

    your are welcome. :-)

  9. bcworkz
    Member
    Posted 10 months ago #

    @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.

Reply

You must log in to post.

About this Topic