[Resolved] custom excerpt length for custom post type

Home Forums Support [Resolved] custom excerpt length for custom post type

Home Forums Support custom excerpt length for custom post type

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #307760
    Susanne

    Hi Tom, Using GP deluxe again for another site I am working on. I have to tell you this theme is the most beautiful thing to work with and I love the documentation too. Thank you for this amazing work!

    I have created a child theme and a custom post type (portfolio) and made the portfolio archive use 3 columns while the blog has one. So far so good.

    Now I want the portfolio archive to show an excerpt length of 20 words, while the blog has an excerpt of 40. I could not find the filter/function for that anywhere, but i know it is doable. The customize settings affect the blog only. What would the filter for my portfolio excerpt be?

    Thanks in advance!

    #307903
    Jamal

    Hi Susanne

    Please take a look at this topic https://isabelcastillo.com/different-excerpt-length-for-different-post-types. If that doesn’t work for you maybe this topic can shed some light https://generatepress.com/forums/topic/excerpt-length-in-elementor/#post-285109

    Hope you get something working and share you code.

    #307993
    Tom
    Lead Developer
    Lead Developer

    That first link is perfect, Jamal πŸ™‚

    #308021
    Susanne

    Thank you both, Jamal and Tom. Perfect!
    First class service as usual.

    #308188
    Susanne

    … actually it looks perfect, but it does not work. Maybe it interferes with the preferences set by the blog plugin? But I need those.. any idea why it would not work? I added the script like so to my functions in the child theme:

    function isacustom_excerpt_length($length) {
        global $post;
        if ($post->post_type == 'post')
        return 25;
        else if ($post->post_type == 'portfolio')
        return 15;
        else
        return 25;
        }
        add_filter('excerpt_length', 'isacustom_excerpt_length');
    #308218
    Tom
    Lead Developer
    Lead Developer

    Can you try replacing:

    add_filter('excerpt_length', 'isacustom_excerpt_length');

    With:

    add_filter('excerpt_length', 'isacustom_excerpt_length', 100);

    #308229
    Susanne

    Brilliant! That did it. very exciting. Thanks so much Tom!

    #308232
    Tom
    Lead Developer
    Lead Developer

    You’re welcome πŸ™‚

    #894172
    Henry

    How would I make this zero? I guess by adding ‘0’?

    I have exactly the same question but in my example, I’d like to remove the excerpt completely.

    Thanks!

    // CHANGE EXCERPT LENGTH FOR DIFFERENT POST TYPES
     
        function isacustom_excerpt_length($length) {
        global $post;
        if ($post->post_type == ' CUSTOM POST TYPE')
        return 0;
        else if ($post->post_type == 'products')
        return 65;
        else if ($post->post_type == 'testimonial')
        return 75;
        else
        return 80;
        }
        add_filter('excerpt_length', 'isacustom_excerpt_length');
    #894340
    Leo
    Staff
    Customer Support

    Yup 0 should work: https://docs.generatepress.com/article/excerpt_length/#examples

    Have you tried it and it’s not working?

    #894821
    Henry

    Alas, this didn’t work. Is is because the syntax is incorrect for my purpose? I am referring to Custom Taxonomy – i.e, categories so perhaps the syntax needs to be tweaked to reflect that?

    Thanks for all help!!!

    #895138
    Henry

    Here is an update!

    OK – the functions on this page didn’t work for me.

    Just to re-clarify – what I am trying to do is REMOVE the excerpts from a Custom Taxonomy Category class I built using Pods.

    I discovered this GP Hook which looks ideal to solve my problem:

    add_filter( 'excerpt_length','lh_custom_category_excerpt_length', 1000 );
    function lh_custom_category_excerpt_length( $length ) 
    {
        if ( is_category() ) {
            return 0;
        } 
        return $length;
    }

    Now, I am using custom taxonomy so I thought I could change the above to this:

    if ( is_tax('CUSTOM-NAME') )

    Alas, no joy.

    Then that got me thinking that the error lies here:

    lh_custom_category_excerpt_length

    ^ That contains ‘category’ which I think might be wrong because I am trying to remove excerpts from custom taxonomy?

    Does anyone have any ideas?

    Thanks!!!

    #895141
    Henry

    UPDATE

    I got it working! πŸ™‚

    This worked for me:

    add_filter( 'excerpt_length','lh_custom_taxonomy_excerpt_length', 1000 );
    function lh_custom_taxonomy_excerpt_length( $length ) 
    {
        if ( is_tax('country') ) {
            return 0;
        } 
        return $length;
    }

    Does anyone see any issue with this?

    #895400
    Leo
    Staff
    Customer Support

    Looks good to me πŸ™‚

    The function name doesn’t matter. They just need to match.

    It could be anything like

    add_filter( 'excerpt_length','henry_is_cool', 1000 );
    function henry_is_cool( $length ) 
    {
        if ( is_tax('country') ) {
            return 0;
        } 
        return $length;
    }

    Glad you figured out!

    #895776
    Henry

    Amazing support as ever. Thanks – solved it.

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.