[Resolved] Add excerpt to Archives page

Home Forums Support [Resolved] Add excerpt to Archives page

Home Forums Support Add excerpt to Archives page

  • This topic has 17 replies, 3 voices, and was last updated 1 week, 1 day ago by David.
Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #2499518
    Chad Biggs

    I created some custom fields using ACF Pro and placed the code in my child theme’s single.php page. I need to display excerpts of the custom fields from the single.php to the archives page. I think the normal way to do this is to change php the_content to php the_excerpt on the archives page and add some code to the functions page but the_content is not on the GP archives page. How can I add excerpts to the archives page from ACF single.php? Thank you.

    #2499530
    Fernando
    Customer Support

    Hi Chad,

    To clarify, is the excerpt you want to add to the archive pages a custom field from ACF?

    If so, might I suggest using a Block Element – Content Template for the archive page, and then adding this custom field through a GB Headline Block’s dynamic setting if possible? Reference: https://docs.generatepress.com/article/block-element-content-template/

    In this way, you wouldn’t need to alter template files.

    Another way is to simply hook the field to articles on the archive page.

    I would personally use a Content Template because it’s much easier to control.

    #2501438
    Chad Biggs

    With the blocks, I can pull in the Post title, author and date but I cant get the excerpt text to populate. Is there a way to target a specific class using the blocks? Or is there a line I can add to my single.php page?

    #2501742
    David
    Staff
    Customer Support

    Hi there,

    in the Headline Blocks Dynamic Data options you can set the Content Source to Post Meta and add the ACF Fieldname in the field provided.
    NOTE: it won’t work with repeater fields.

    But …. does the ACF fields content need trimming to an excerpt ?

    #2502044
    Chad Biggs

    I have dynamic data set to post meta and the field name is entered. It all looks right to me compared to what I read from you but it’s still not working. The field I am trying to get is a text field, not a repeater.
    block excerpt settings

    #2502250
    Fernando
    Customer Support

    Hi Chad,

    From your screenshot, I can see that you’re not using a GB Headline Block.

    Here’s an example of how to do it: https://share.getcloudapp.com/WnuZpJGr

    Reference: https://docs.generateblocks.com/article/headline-overview/#dynamic-data

    With that said, can you try retrieving it through a GB Headline Block?

    #2503256
    Chad Biggs

    Thank you. Is there a way to limit the characters to 55-60 in the excerpt?

    #2503336
    David
    Staff
    Customer Support

    If you’re display the post Excerpt in the Headlines Dynamic data, then there is an option to change its length.
    Check the front end if you don’t see a change in the editor

    If you want to trim the length of the custom field, then you would need to do that using a shortcode. See here:

    https://support.advancedcustomfields.com/forums/topic/trimming-words-from-a-field/#post-149508

    Then the shortcode can be added inside a Headline Block

    #2505317
    Chad Biggs

    I do not see an option to change the excerpt length under the dynamic headline in the elements. I have attached a snapshot of my list view group layout in case I am doing something wrong.

    By “Check the front end if you don’t see a change in the editor”, I guess you mean I can set the excerpt length in customizing/layout/blog/ content type. But changing any settings under content type does not make any changes.

    elements group list view

    #2505369
    Fernando
    Customer Support

    Hi Chad,

    Try adding trim-custom-description to the class list of the Headline Block.

    Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    Then, add this snippet:

    add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
    	if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'trim-custom-description' ) !== false ) {
    		$content = wp_trim_words( $content, 2, '...' );
    	}
    	return $content;
    }, 10, 3);

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    Change 2 in wp_trim_words( $excerpt, 2, '...' ) to the preferred excerpt length.

    #2505766
    Chad Biggs

    Thank you for your help!

    #2506550
    Fernando
    Customer Support

    You’re welcome, Chad!

    #2507117
    Chad Biggs

    Do you know if there is a way to change that code to limit characters instead of words?

    #2507136
    David
    Staff
    Customer Support

    Hi there,

    try changing this:

    $content = wp_trim_words( $content, 2, '...' );

    to:

    
    $stripHTML = strip_tags($content);
    $content = substr($stripHTML, 0, 15);
    
    #2507389
    Chad Biggs

    That works great but I have tried every scenario I can think of to get the ‘…’ behind the last word like the other code had. It is possible?

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