- This topic has 23 replies, 3 voices, and was last updated 3 years, 6 months ago by
David.
-
AuthorPosts
-
October 22, 2022 at 2:01 am #2381898
Magnus
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
October 22, 2022 at 6:09 am #2382054David
StaffCustomer SupportHi there,
you can use the
render_blockfilter 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.October 22, 2022 at 7:00 am #2382096Magnus
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?
October 22, 2022 at 11:33 am #2382453Ying
StaffCustomer SupportCan you share the exact code you are using?
October 22, 2022 at 12:07 pm #2382489Magnus
Hello Ying,
Do you need the complete functions.php?
October 22, 2022 at 12:37 pm #2382496Ying
StaffCustomer SupportI 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-2382054Let me know!
October 22, 2022 at 2:46 pm #2382568Magnus
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 );October 23, 2022 at 4:11 am #2382800David
StaffCustomer SupportSelect the Headline block where the summary is displayed, and in Advanced > Additional CSS Class(es) add:
post-summaryNOTE 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.
October 24, 2022 at 1:22 am #2383780Magnus
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?
October 24, 2022 at 3:51 am #2383919David
StaffCustomer SupportCan you provide a screenshot of the issue that it causes ?
October 24, 2022 at 4:10 am #2383949Magnus
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
October 24, 2022 at 5:21 am #2384005David
StaffCustomer SupportHmmm…. 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?October 24, 2022 at 5:29 am #2384017Magnus
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
October 24, 2022 at 6:15 am #2384059David
StaffCustomer SupportThinking 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 );October 24, 2022 at 6:54 am #2384106Magnus
No, unfortunately no limit of characters…
-
AuthorPosts
- You must be logged in to reply to this topic.