- This topic has 12 replies, 2 voices, and was last updated 3 years, 3 months ago by
Fernando.
-
AuthorPosts
-
December 28, 2022 at 7:15 am #2475692
Kees
Hi,
Is it possible to make a placeholder/fallback for a dynamic field? I have a video in the private data.
Sometimes the field is empty so i would like to show a placeholder or default/fallback text.How dow we fix this?
Regards,
NiekDecember 28, 2022 at 6:25 pm #2476318Fernando Customer Support
Hi Niek,
Yes, it’s possible.
1. Give the Headline Block a class of
cu-headlineAdding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/
2. Add this Snippet:
add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){ if ( !is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-headline' ) !== false ) { if( ! $content ){ $content = 'MY TEXT HERE'; } } return $content; }, 10, 3);Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
Replace “MY TEXT HERE” with your fallback text.
December 29, 2022 at 1:25 am #2476580Kees
Hi,
Thanks, but this is not working. I think i did everything right. See video. Does the code needs to be changed?
December 29, 2022 at 1:29 am #2476585Fernando Customer Support
Sorry, I forgot to mention that you need to use the dynamic settings of GB to make this code work.
Can you remove the dynamic settings from the toolbar? Then, enable the dynamic settings from the Block Setting on the side. This one is from GenerateBlocks.
Let us know how it goes.
December 29, 2022 at 1:33 am #2476587Kees
Nice, that seems to work. We have several dynamic fields. What is the best way? copy the code and change the name of the function and class?
December 29, 2022 at 1:45 am #2476590Kees
So, for each heading we need a fallback
December 29, 2022 at 1:50 am #2476593Fernando Customer Support
You can try to modify the code to something like this:
add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){ if ( ! is_admin() && ! $content && ! empty( $attributes['className'] ) ) { if( strpos( $attributes['className'], 'cu-headline' ) !== false ){ $content = 'MY TEXT HERE'; } else if ( strpos( $attributes['className'], 'cu-headline2' ) !== false ){ $content = 'MY TEXT HERE 2'; } else if ( strpos( $attributes['className'], 'cu-headline3' ) !== false ){ $content = 'MY TEXT HERE 3'; } } return $content; }, 10, 3);You can copy and paste the else if condition multiple times for each of your dynamic text. You need to have different classes for each.
December 29, 2022 at 1:53 am #2476595Kees
Thanks, almost there i think. But the output is then always the same: see video.
December 29, 2022 at 2:00 am #2476598Fernando Customer Support
I see. Can you try a different structure for the Headline classes?
For instance,
ab-headline,cd-headlineandef-headline.December 29, 2022 at 2:04 am #2476600Kees
Nice, that works. Is that ab- cd- ef- random? or did your search somewhere for that?
December 29, 2022 at 2:09 am #2476606Fernando Customer Support
It’s random. Just checked if
strposis strict comparison. Since ab- cd -ef works, it seems it isn’t.Using different characters for your variables at the start is ideal for the code to work.
December 29, 2022 at 2:10 am #2476608Kees
Thanks, will try that. For now it is solved.
December 29, 2022 at 2:13 am #2476610Fernando Customer Support
You’re welcome, Niek!
-
AuthorPosts
- You must be logged in to reply to this topic.