-
linuspress
Hi,
Is it possible for a part of a synced pattern to not be synced?
I have a button with dynamic data that references an AFC called price_tear_trough.
I want to change that dynamic data freely while still keeping it a synced pattern.Example: on the page /tear-trough/, i want to reference price_tear_trough
On /smootheye/, I want to be able to change the reference freely to something else, such as price_smootheyeIs this possible or are there perhaps another approach to this?
The issue is otherwise i have to create one local pattern per page, instead of having only one local pattern -
David
Hi there,
partially synced patterns isn’t a thing yet.. WordPress are working on that , although i am not 100% it would handle this requirement.
So you would need to do something to workaround that.
For example:
1. add a Custom Field to the page to store the “slug” eg.
tear_trough
orsmootheye
2. add a GB Headline block and
2.1 set its Dynamic Data to display that custom field so it actually shows that value on the front end.
2.2 give it an Advanced > Additional CSS Class of:dynamic-meta
3. Now a PHP Snippet:
add_filter( 'render_block_generateblocks/container', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'dynamic-meta' ) !== false ) { $meta_field_name = 'price_' . $block_content; $meta_field_value = get_field( $meta_field_name , get_the_ID()); if ( $custom_field ) { $block_content = $meta_field_value; } } return $block_content; }, 10, 2 );
This should use the dynamic data in the headlibe block to build a new get_field request to return the value you need.
-
linuspress
Hi David,
Thanks a lot for the reply.
The website is live here: https://hittaklinik.se/behandling/tear-trough/
1. I have a custom field called pris_tear_trough
2. I’m probably doing this step incorrectly.
I have a GB Headline block with dynamic data.
I added meta key tear_trough and class dynamic-meta.(Currently, it does not show any numbers, but if I change the custom field to pris_tear_trough, it shows the correct number.)
3. I use the following snippet with pris_ as meta key name
add_filter( ‘render_block_generateblocks/container’, function( $block_content, $block ) {
if (
!is_admin() &&
! empty( $block[‘attrs’][‘className’] ) &&
strpos( $block[‘attrs’][‘className’], ‘dynamic-meta’ ) !== false
) {
$meta_field_name = ‘pris_’ . $block_content;
$meta_field_value = get_field( $meta_field_name , get_the_ID());
if ( $custom_field ) {
$block_content = $meta_field_value;
}
}return $block_content;
}, 10, 2 );Please let me know if you see what I’m doing wrong. Thank you.
-
David
Lets roll back a moment.
On every page – are you going to have a different Custom Field to store the value specific to that page ?
If so – why do you need a different custom field name ? -
linuspress
Yes, a different custom field for each page.
I want a different custom field name because I want to display different prices on pages.
For /tear-trough/, I want to display the price of a tear trough treatment, stored under pris_tear_trough
For /smootheye/, I want the price of smootheye, stored under pris_smootheye.If I can dynamically change the custom field (tear_trough, smootheye etc) depending on the slug, I would only need one synced pattern, instead of one pattern per treatment.
Thanks!
-
David
Try this instead:
1. remove any code from above:
2. add this php snippet:add_filter( 'render_block_generateblocks/headline', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'dynamic-meta' ) !== false ) { global $post; $slug = $post->post_name; $meta_field_name = 'pris_' . $slug; $meta_field_value = get_field( $meta_field_name , get_the_ID()); if ( $meta_field_value ) { $block_content = $meta_field_value; } } return $block_content; }, 10, 2 );
What this code does is generates the custom field name with the
pris_
prefix + the current post name.2. Add a GB Headline block and just give it a CSS Class of:
dynamic-meta
-
linuspress
This would be a perfect solution. I get Undefined variable $custom_field on line 11
-
Hi there,
Change
$custom_field
to$meta_field_value
in line 11 of the code.Let me know if this helps!
-
linuspress
Hi, I tried but unfortunately it did not work. Any other ideas? Thanks!
-
I updated my code above, can you try that ?
-
linuspress
Thanks, I tried but no success so far. Perhaps I am implementing it incorrectly, feel free to have a look
-
David
Ah ok… the issue here is that snippet gets the slug of the current post in the loop.
So its going to generated the wrong custom field name.
Change the snippet to this:// store the current page for use in the loop add_action('wp', function() { global $current_page; $current_page = get_queried_object(); }); add_filter('render_block_generateblocks/headline', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'dynamic-meta' ) !== false ) { global $current_page; if ( isset($current_page) && $current_page instanceof WP_Post ) { $page_slug = $current_page->post_name; $meta_field_name = 'pris_' . $page_slug; $meta_field_value = get_field( $meta_field_name, get_the_ID() ); if ( $meta_field_value ) { $block_content = $meta_field_value; } } } return $block_content; }, 10, 2);
This should get and store the current page object so we can then use its name inside the loop.
-
linuspress
It works! You really provide the best support. Thank you very much.
-
David
Glad to hear that !!
- You must be logged in to reply to this topic.