Site logo

Archived topic

Add excerpt to Archives page

17 replies · Started by Chad Biggs on January 17, 2023

Viewing posts 1–15 of 18

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.

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.

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?

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 ?

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

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

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

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.

Thank you for your help!

You're welcome, Chad!

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

Hi there,

try changing this:

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

to:


$stripHTML = strip_tags($content);
$content = substr($stripHTML, 0, 15);

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?

This archived topic is closed to new replies.