Viewing 29 posts - 1 through 29 (of 29 total)
  • Author
    Posts
  • #1373805

    Hi I have this for portfolio grid –

    add_filter('avf_portfolio_grid_excerpt', function($excerpt, $entry) {
       $excerpt = get_field( 'the_project', $entry->ID );
       return $excerpt; 
    }, 10, 2);

    How do I do the same for blog posts? I tried:

    add_filter('avf_blog_posts_excerpt', function($excerpt, $entry) {
       $excerpt = get_field( 'the_project', $entry->ID );
       return $excerpt; 
    }, 10, 2);

    But it didn’t work – anyone know the correct filter?

    Thanks in advance.

    #1373831

    Hey domchocolate,

    Thank you for the inquiry.

    You can use the get_the_excerpt filter to adjust the excerpt of the default posts.

    // https://developer.wordpress.org/reference/hooks/get_the_excerpt

    Make sure to check for the post type.

    Best regards,
    Ismael

    #1373837

    Sorry – I’m no expert. Can you elaborate? How do I add that to my code above? The excerpt needs to be replaced by a custom field.

    Thanks in advance

    Dominic

    #1373844

    Hi Dominic,

    Please try to use this filter:

    // Use custom excerpt for Blogposts for grid layout
    add_filter("avf_post_slider_entry_excerpt", function($excerpt, $prepare_excerpt, $permalink, $entry, $class) {
    	$excerpt = get_field( 'the_project', $entry->ID );
       	return $excerpt; 
    }, 10, 5);
    
    // Use custom excerpt for Blogposts in all layout except grid
    add_filter('get_the_excerpt', function($excerpt, $entry) {
       $excerpt = get_field( 'the_project', $entry->ID );
       return $excerpt; 
    }, 10, 2);

    Hope this helps.

    Best regards,
    Nikko

    #1373864

    Thanks Nikko – that’s great. And works a treat. Is there a way to put a word count – say the first 50 words and then add a “…” at the end or a [READ MORE]??

    #1374220

    Hi domchocolate,

    Please try to replace the last code I gave with this one:

    function modify_excerpt($excerpt) {
       $label = "Read More";
       $limit = 50;
       $excerpt = string_limit_words( $excerpt, $limit) . '…';
       $excerpt .= '<a href="" class="avia-button av-readmore avia-icon_select-no avia-size-small avia-position-center avia-color-theme-color"><span class="avia_iconbox_title">' . $label . '</span></a>';
       return $excerpt;
    }
    
    function string_limit_words($string, $word_limit) {
       $words = explode(' ', $string, ($word_limit + 1));
    	
       if(count($words) > $word_limit) {
          array_pop($words);
       }
    	
       return implode(' ', $words);
    }
    
    // Use custom excerpt for Blogposts for grid layout
    add_filter("avf_post_slider_entry_excerpt", function($excerpt, $prepare_excerpt, $permalink, $entry, $class) {
       $custom_excerpt = get_field( 'the_project', $entry->ID );
    	
       if ( $custom_excerpt ) {
          $excerpt = modify_excerpt($custom_excerpt);
       }
    	
       return $excerpt; 
    }, 10, 5);
    
    // Use custom excerpt for Blogposts in all layout except grid
    add_filter('get_the_excerpt', function($excerpt, $entry) {
       $custom_excerpt = get_field( 'the_project', $entry->ID );
    	
       if ( $custom_excerpt ) {
          $excerpt = modify_excerpt($custom_excerpt);
       }
    	
       return $excerpt; 
    }, 10, 2);

    Just change the label and limit as you see fit.
    Hope this helps.

    Best regards,
    Nikko

    #1374259

    Hi Nikko – thanks for this but no cigar I’m afraid. It’s pulling the excerpt from the_project through but not cutting it to 50 words.

    See links below and PW in private content.

    Masonry portfolio element:
    https://owe.brother.design/project-item/oxford-station-western-side/

    Blog post element:
    https://owe.brother.design/sites/oxpens-phase-1/

    Thanks in advance
    Dominic

    #1374300

    Also is it possible to use the excerpt from an excerpt field when the_project either hasn’t been utilised or isn’t on another post type?
    In other words:
    Post types I have =
    Portfolio items – use the_project custom field as excerpt
    Sites – use the_project custom field as excerpt
    Posts – doesn’t have the the_project custom field so use excerpt field for the excerpt.

    Thanks in advance.

    #1374852

    Hi domchocolate,

    I apologize for the delayed response.
    I have tested the code and it works, can you create a staging site? where we can try to check why it’s not working on your end.
    Also, please give us temporary admin access on it.

    Also is it possible to use the excerpt from an excerpt field when the_project either hasn’t been utilised or isn’t on another post type?
    Yes, it’s possible that why there’s a code to check it:

    if ( $custom_excerpt ) {
       $excerpt = modify_excerpt($custom_excerpt);
    }

    Best regards,
    Nikko

    #1375099

    Hi Nikko

    Basically, it works for the posts element, see – https://owe.brother.design/sites/oxpens-phase-1/ under heading Other development opportunities when the post element is calling a custom post and using the_project as an excerpt. But for some reason not in the second post element on the same page under the heading Related news and updates – this one uses a post element calling standard posts from the blog and using a standard excerpt field (doesn’t shorten the excerpt to 50 words or add [more info].

    Also, it doesn’t work at all for portfolio grids, see – https://owe.brother.design/project-item/oxford-station-western-side/ under heading Other projects.

    Code added as Code snippet

    Any thoughts?

    #1375101

    BTW I sorted the portfolio grid by using the post element to call portfolio items instead.

    #1375106

    One other thing – I created this test page. This uses blog posts element throughout (no portfolio grids). https://owe.brother.design/excerpt-test/

    Blog post element 1 – calls standard posts and uses the standard excerpt field. This element is not working as I’d like because it does not shorten the excerpt or add ‘… Read more >’.
    Blog post element 2 – calls portfolio posts and uses the_project custom field as excerpt.

    You’ll also notice that when the excerpt is longer than 50 words and the excerpt is edited then the ‘… Read more’ works correctly by being inserted after the 50th word.

    If excerpt is shorter than 50 words then the ‘… Read more’ is after a line break which is inconsistent and not as desired. Also when the excerpt is shorter than 50 words it’s displaying the full excerpt and therefore the ‘…’ is not actually necessary or grammatically accurate. Can we remove the ‘…’ under those parameters and display the ‘Read more’ directly after the last word of the excerpt?

    Does that makes sense? And thanks in advance.

    Dominic

    • This reply was modified 1 year, 11 months ago by domchocolate.
    #1376122

    Hi – just checking in. Any thoughts?

    #1376363

    Hi domchocolate,

    I apologize for the delayed response.
    You can remove the ‘…’ by changing this code:

    $excerpt = string_limit_words( $excerpt, $limit) . '…';
    $excerpt .= '<a href="" class="avia-button av-readmore avia-icon_select-no avia-size-small avia-position-center avia-color-theme-color"><span class="avia_iconbox_title">' . $label . '</span></a>';

    to:

    $excerpt = string_limit_words( $excerpt, $limit);
    $excerpt .= ' <a href="" class="avia-button av-readmore avia-icon_select-no avia-size-small avia-position-center avia-color-theme-color"><span class="avia_iconbox_title">' . $label . '</span></a>';

    and to make sure that read more is added after the paragraph, you can add this CSS code in Quick CSS:

    #top .avia-content-slider .slide-entry-excerpt.entry-content p {
        display: inline;
    }

    Hope it helps.

    Best regards,
    Nikko

    #1376371

    maybe a function like this is helpful – if you got your excerpt
    ( i do not see your site – so no examination of the DOM for me is possible – you had to adapt the selectors yourself )
    f.e. if you have the grid layout for blog …

    (function($){
    // trim excerpt by words
        function trimByWord(sentence,wordcount = 50) {
            var result = sentence;
            var resultArray = result.split(" ");
            if(resultArray.length > wordcount){
            resultArray = resultArray.slice(0, wordcount);
            result = resultArray.join(" ") + " … Read more";
            }
            return result;
        }
        $(document).ready(function(){
            $('.avia-content-slider .slide-entry').each(function() {
                 $('.slide-entry-excerpt.entry-content').text(function(index, currentText) {
                    return trimByWord(currentText);
                 });
            });
        });
        
    })(jQuery);

    and here: " … Read more" you can choose your addendum

    #1376416

    Thanks, Nikko

    That CSS addressed the problem on the read more issue – see second blog post element on page https://owe.brother.design/excerpt-test/. This element displays portfolio or custom posts that use the_project custom field as the excerpt.

    The first blog post element on the page https://owe.brother.design/excerpt-test/ – uses the element to pull in standard posts and uses the standard excerpt field on these posts. You can see that the excerpt is neither shortened to 50 words nor has the read more.

    Any thoughts how to fix it in that scenario?

    #1376419

    Hi Guenni and thanks for this.

    I’m afraid this code has a syntax error somewhere (picked up by WPCode snippets. Any ideas?

    Plus the website details are listed below in the private content.

    The example page – https://owe.brother.design/excerpt-test/ – shows two post blog elements :

    The top one displays standard posts and uses the standard WP excerpt field. This isn’t shortening the excerpt to 50 words.

    The second example uses the same blog post element to display a custom post type (in this case Portfolio) and uses Nikko’s code above to use the text found in ‘the_project’ custom field as the excerpt (using Nikko’s code above). This does exactly what I want and shortens the excerpt.

    Basically, I want the top one to act like the second one.

    Does that make any sense?

    #1376455

    attention: this is only the jQuery function – if you like to use it – you had to build a php snippet of it.
    but if the filter snippets of Nikko works – these will be a better way to do it. – Because it’s best to hook into the generation of those excerpts than to change them afterwards.

    #1377451

    Hi Nikko or anyone else – any luck with this???

    #1377459

    OK – I got this working (almost). So I removed the copy from the excerpt field on the standard post and then the excerpt got pulled from the content field of the post. Perfect. Except it cuts the excerpt off at 9 words (10 including the ellipses (…) ). Can we get this to do 50 words the same as when we use the_project custom field as excerpt?

    Example using standard posts: https://owe.brother.design/updates-and-events/

    Example using custom post and the_project custom field: https://owe.brother.design/projects/

    Thanks in advance

    Dominic

    #1377532

    Hi Dominic,

    Please try to increase the excerpt length by adding this code in functions.php:

    function custom_postgrid_excerpt_length($excerpt_length) {
    	$excerpt_length = 250;
    	return $excerpt_length;
    }
    add_filter( 'avf_postgrid_excerpt_length', 'custom_postgrid_excerpt_length');

    Then replace these codes:

    // Use custom excerpt for Blogposts for grid layout
    add_filter("avf_post_slider_entry_excerpt", function($excerpt, $prepare_excerpt, $permalink, $entry, $class) {
       $custom_excerpt = get_field( 'the_project', $entry->ID );
    	
       if ( $custom_excerpt ) {
          $excerpt = modify_excerpt($custom_excerpt);
       }
    	
       return $excerpt; 
    }, 10, 5);
    
    // Use custom excerpt for Blogposts in all layout except grid
    add_filter('get_the_excerpt', function($excerpt, $entry) {
       $custom_excerpt = get_field( 'the_project', $entry->ID );
    	
       if ( $custom_excerpt ) {
          $excerpt = modify_excerpt($custom_excerpt);
       }
    	
       return $excerpt; 
    }, 10, 2);

    to:

    // Use custom excerpt for Blogposts for grid layout
    add_filter("avf_post_slider_entry_excerpt", function($excerpt, $prepare_excerpt, $permalink, $entry, $class) {
       $custom_excerpt = get_field( 'the_project', $entry->ID );
    	
       if ( $custom_excerpt ) {
          $excerpt = $custom_excerpt;
       }
    	
       return modify_excerpt($excerpt); 
    }, 10, 5);
    
    // Use custom excerpt for Blogposts in all layout except grid
    add_filter('get_the_excerpt', function($excerpt, $entry) {
       $custom_excerpt = get_field( 'the_project', $entry->ID );
    	
       if ( $custom_excerpt ) {
          $excerpt = $custom_excerpt;
       }
    	
       return modify_excerpt($excerpt); 
    }, 10, 2);

    Hope this helps.

    Best regards,
    Nikko

    #1377687

    Thanks, Nikko – Brilliant! That works! One tiny issue…

    On the custom posts that use the custom field (the_project) as an excerpt there is one set of ellipses (…) before read more link. eg. https://owe.brother.design/projects/

    On the standard posts that use the content as the excerpt there are two sets of ellipses (.……) before read more link. eg. https://owe.brother.design/updates-and-events/

    Any ideas on how we fix that so we only get one set on each?

    Thanks in advance

    • This reply was modified 1 year, 10 months ago by domchocolate.
    #1377819

    Hi Nikko

    I noticed another issue with the read more links not linking to their respective post or custom post.

    function modify_excerpt($excerpt) {
       $label = "Read More";
       $limit = 50;
       $excerpt = string_limit_words( $excerpt, $limit) . '…';
       $excerpt .= '<a href="" class="avia-button av-readmore avia-icon_select-no avia-size-small avia-position-center avia-color-theme-color"><span class="avia_iconbox_title">' . $label . '</span></a>';
       return $excerpt;
    }

    Should there not be something here:

    <a href="call the post permalink" class="avia-button av-readmore avia-icon_select-no avia-size-small avia-position-center avia-color-theme-color">

    eg: href="'. get_permalink($post->ID) . '"??? (I tried this one and it didn’t work).

    Thanks in advance

    • This reply was modified 1 year, 10 months ago by domchocolate.
    • This reply was modified 1 year, 10 months ago by domchocolate.
    • This reply was modified 1 year, 10 months ago by domchocolate.
    #1377823

    maybe use: get_permalink($entry->ID)

    #1377824

    Thanks, Guenni

    Like this?

    function modify_excerpt($excerpt) {
       $label = "Read more";
       $limit = 50;
       $excerpt = string_limit_words( $excerpt, $limit) . '… ';
       $excerpt .= '<a href="'get_permalink($entry->ID)'"><span class="avia_iconbox_title">' . $label . '<span class="more-link-arrow"></span></span></a>';
    
       return $excerpt;
    }

    This returns an error “We encountered an error activating your snippet, please check the syntax and try again.”

    • This reply was modified 1 year, 10 months ago by domchocolate.
    #1377881

    Any luck? Anyone? I’m rather keen to fix this because I have a client waiting for this particular request. Sorry to be a pain.

    #1377934

    It’s hard to give advice without seeing the object at hand.
    A bit like I have to make a diagnosis without seeing the patient.
    But you should try the binding points, as you indicated above.
    ____________
    By the way, the more-link-arrow is mostly hidden in Enfold – so you could leave it out – whereas I would include the possibility of translating “Read more”.
    Then you don’t need the$label = "Read More"; line

    Maybe this way:

    '<a class="more-link"  href="'.get_permalink($entry->ID).'"><span class="avia_iconbox_title">' . __( 'Read more', 'avia_framework' ) . '</span></a>';
    
    #1377942

    Thanks, @guenni007 – that didn’t work I’m afraid.

    But I did find the solution in the end. For those interested it’s:

    function string_limit_words( $string, $word_limit )
    {
        $words = explode( ' ', $string, ( $word_limit + 1 ) );
    
        if ( count( $words ) > $word_limit )
        {
            array_pop( $words );
        }
    
        return implode( ' ', $words );
    }
    
    function modify_excerpt( $excerpt, $entry )
    {
        $label   = "Read more";
        $limit   = 50;
        $excerpt = string_limit_words( $excerpt, $limit ) . '… ';
        $excerpt .= '<a href="' . get_permalink( $entry->ID ) . '"><span class="avia_iconbox_title">' . $label . '<span class="more-link-arrow"></span></span></a>';
    
        return $excerpt;
    }
    
    // Use custom excerpt for Blogposts for grid layout
    add_filter( "avf_post_slider_entry_excerpt", function ( $excerpt, $prepare_excerpt, $permalink, $entry, $class ) {
        $custom_excerpt = get_field( 'the_project', $entry->ID );
    
        if ( $custom_excerpt )
        {
            $excerpt = $custom_excerpt;
        }
    
        return modify_excerpt( $excerpt, $entry );
    }, 10, 5 );
    
    // Use custom excerpt for Blogposts in all layout except grid
    add_filter( 'get_the_excerpt', function ( $excerpt, $entry ) {
        $custom_excerpt = get_field( 'the_project', $entry->ID );
    
        if ( $custom_excerpt )
        {
            $excerpt = $custom_excerpt;
        }
    
        return modify_excerpt( $excerpt, $entry );
    }, 10, 2 );
    

    Thanks again everyone for all your help.

    Dominic

    #1377955

    Hi,
    Glad to hear that you have this sorted out, and thanks for sharing your solution if you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.

    Best regards,
    Mike

Viewing 29 posts - 1 through 29 (of 29 total)
  • The topic ‘Using a custom field as excerpt’ is closed to new replies.