- This topic has 16 replies, 3 voices, and was last updated 3 years, 4 months ago by
David.
-
AuthorPosts
-
December 14, 2022 at 12:14 pm #2462027
Cris
Hello
I am creating a page to display custom fields made with ACF. I use the dynamic options of the Headline block to show the names of those custom fields but in another block I want to also show the field label so that, in case there is no data, it is not displayed. Can you tell me how can I do it please?
December 14, 2022 at 2:06 pm #2462134Ying
StaffCustomer SupportHi Cris,
I would recommend using a static headline to show the label.
Then add a CSS class to the static headline, eg.
hide-when-emptyand the below PHP snippet, when the post meta field is empty, the static headline will not appear. Please replace thepost_meta_field_namewith your post meta field name.add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'hide-when-empty' ) !== false && empty (get_field('post_meta_field_name'))){ $block_content = ""; } return $block_content; }, 10, 2 );Adding PHP: https://docs.generatepress.com/article/adding-php/
December 15, 2022 at 1:52 am #2462490Cris
Thanks Ying!
It works perfect but I want to make it for 8 different field labels. Do I repeat the process for each of them or is there another way?December 15, 2022 at 6:32 am #2462753David
StaffCustomer SupportHi there,
is this in a GP Element ?
December 15, 2022 at 6:39 am #2462761Cris
Hi David,
Yes, this is a Block Element of GP
December 15, 2022 at 7:00 am #2462785David
StaffCustomer SupportIn the GP Element the Container Block has a special option called:
REMOVE CONTAINER CONDITIONin its Layout Settings.
You can set it toNo Post Metaand add you ACF Field name. Then use that as a wrapper for your Label and Field.December 15, 2022 at 7:24 am #2462825Cris
That works great! I could even put anything inside the block and it would still disappear.
Thanks a lot, David!December 15, 2022 at 7:38 am #2462860Cris
One more thing:
One of the custom fields contains emails and when displayed I would like it to do a “mailto:”
It could be done?December 15, 2022 at 7:47 am #2462877David
StaffCustomer SupportTry this PHP Snippet:
function db_rerender_email_url( $block_content, $block ) { if(!is_admin()){ if ( ! empty( $block['attrs']['className'] ) && 'is-email' === $block['attrs']['className'] ) { $my_search='href="'; $my_replace='href="mailto:'; $new_content = str_replace($my_search, $my_replace, $block_content); return $new_content; } } return $block_content; } add_filter( 'render_block', 'db_rerender_email_url', 10, 2 );And the block that has the email field give it a class of
is-emailDecember 15, 2022 at 8:03 am #2463033Cris
It does not work
December 15, 2022 at 8:08 am #2463037David
StaffCustomer SupportDid you give the block the class of
is-email?December 15, 2022 at 8:21 am #2463055Cris
Yes, I tried in the container block and in the headline block where the meta field is
December 15, 2022 at 8:22 am #2463059David
StaffCustomer SupportCan i see the site where this meta is displayed?
December 15, 2022 at 10:28 am #2463197Cris
okay
I’ll send you a link in privateDecember 15, 2022 at 11:11 am #2463251David
StaffCustomer SupportSo that custom field you need to also set it as the Link Source. As the snippet above searches for the
hrefin the<a>and currently you’re just outputting it as static text. -
AuthorPosts
- You must be logged in to reply to this topic.