Archived topic
Limit characters of custom fields displayed with dynamic data
23 replies · Started by Magnus on October 22, 2022
Hello,
I'm using a custom field text block "trail_summary" as an intro text on my blog post, embedded via page hero element, e.g. https://ride-with-love.bike/produkt/transalp-oberstdorf-gardasee-id0004/
The reason why is, that I want to prevent double content on the blog post and there is no function to disable the intro text in the blog post if it's used in the page hero already (disable title in page hero is possible, but not an text element?!?).
I use "Meta-Pages" as a kind of archive pages, e.g. https://ride-with-love.bike/transalp-enduro-bike-beste-routen/ This "Meta-Page" is built mainly on dynamic content sourced from each embedded blog post.
Is there a way to limit the characters of my custom field "trail_summary" if used with dynamic content as a text teaser?
Hopefully it's clear what I'm asking for...
Thanks, Magnus
Hi there,
you can use the render_block filter to change the block content eg.
add_filter( 'render_block', function( $block_content, $block ) {
$summary = get_post_meta( get_the_ID(), 'your_custom_field', true );
if (
!is_admin()
&& is_single()
&& ! empty( $block['attrs']['className'] )
&& 'post-summary' === $block['attrs']['className']
&& $summary
) {
$block_content = wp_trim_words( $summary, 40 );
}
return $block_content;
}, 10, 2 );
This line: $summary = get_post_meta( get_the_ID(), 'your_custom_field', true ); you need to add your_custom_field name.
This line: && 'post-summary' === $block['attrs']['className'] you define a CSS class that you add to the block whose content you want to change.
Hi David,
I've added to code to my functions.php and added my custom field name. But the result on the overview page (e.g. https://ride-with-love.bike/stilfser-joch-die-besten-trails-suedtirols/) is still the same: still the complete text block is shown. No limit of the characters
$block_content = wp_trim_words( $summary, 40 );
And I guess the value 40 should be the amount of characters?
Can you share the exact code you are using?
Hello Ying,
Do you need the complete functions.php?
I just need to see the code adapted from David's for now :)
https://generatepress.com/forums/topic/limit-characters-of-custom-fields-displayed-with-dynamic-data/#post-2382054
Let me know!
Here you Go
add_filter( 'render_block', function( $block_content, $block ) {
$summary = get_post_meta( get_the_ID(), 'trail_summary', true );
if (
!is_admin()
&& is_single()
&& ! empty( $block['attrs']['className'] )
&& 'post-summary' === $block['attrs']['className']
&& $summary
) {
$block_content = wp_trim_words( $summary, 40 );
}
return $block_content;
}, 10, 2 );
Select the Headline block where the summary is displayed, and in Advanced > Additional CSS Class(es) add: post-summary
NOTE we specify that class in this line of the code:
&& 'post-summary' === $block['attrs']['className']
You can can that class in the code and in the block for whatever you require. But note: it should be unique, as that filter will apply to any block on the page with that class.
Sorry, I doesn't work in a correct way:
It sizes the characters, but if I use the CSS for an additional dynamic content element, the content element is overwritten by the one element. So limiting the characters for multiple content blocks on one page with this solution doesn't work. See this page: https://ride-with-love.bike/stilfser-joch-die-besten-trails-suedtirols/
Any idea?
Can you provide a screenshot of the issue that it causes ?
Here is the screenshot: https://www.screencast.com/t/lVbUxMjR
the blue marked areas have all the same content (with limited characters) but it should be different content blocks. So obviously the code is overwriting each individual content id and just showing one/ same content element
Hmmm.... how are you adding the trail summary text to each of the posts?
Are you using ACF or another plugin to add them? Or is it the core WP Custom Fields?
I'm using ACF. Each post is using the custom field "trail_summary" with its own content. Additional the custom field "trail_summary" is used on a kind of "archive page" as teaser for each post. So, this "archive page" has multiple custom fields "trail_summary".
To include the custom field I use "Headline" with the option dynamic content: https://www.screencast.com/t/Y2RtBWZ5Qkf
Thinking about it - try this instead:
add_filter( 'render_block', function( $block_content, $block ) {
if (
!is_admin()
&& is_single()
&& ! empty( $block['attrs']['className'] )
&& 'post-summary' === $block['attrs']['className']
&& $summary
) {
$block_content = wp_trim_words( $block_content, 40 );
}
return $block_content;
}, 10, 2 );
No, unfortunately no limit of characters...