- This topic has 5 replies, 3 voices, and was last updated 3 years, 2 months ago by
Fernando.
-
AuthorPosts
-
January 19, 2023 at 7:15 am #2501496
Ashia
Hello,
Can somebody help me how I can create a Post Title(H1) using two or more custom meta fields,
Example Title: How to cook Indian Chicken Biriyani.
Above – 1. How to cook (Should be Fixed in every title)
2. Indian (Custom meta field, so I can put anything from post editor)
3. Chicken Biryani (Custom meta field, so I can put any recipe I want in post editor)
The title should be within (h1) tag.
I want to create heading 1 using two or more meta fields, then I wish to use that heading in elements post title.
Actually when I am making the heading 1 using ACF, then using Generatepress and Generateblocks elements and its headline block, I am getting a headline 1, but its not perfect, here are the codes I get after publishing…
<div class="gb-container gb-container-f74e60e6"> <h1 class="gb-headline gb-headline-7f1b92d5 gb-headline-text">This is custom meta field 1</h1> <h1 class="gb-headline gb-headline-07e24467 gb-headline-text">this is custom meta field 2</h1> </div>Above, in one div, I got two heading-1 blocks (dynamic meta fields), This div I am using as post title, but the issue here its not in one H1 tag, there are two H1 tags in title, also there is a whitespace between these two H1 blocks.
I want that, two or more than one meta fields should be used in one H1.
January 19, 2023 at 8:12 am #2501734David
StaffCustomer SupportHi there,
it would require some PHP to filter the Headline Block.
1. Add a Headline block.
1.1 DO NOT set any dynamic data
1.2 Give the block a Class of:custom-post-title
the class can be whatever you choose it just needs to be set in the PHP
1.3 Add some static text eg.How to cook {{magic}}
We will replace the{{magic}}string with the custom field using PHP2. Add this PHP :
add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && 'custom-post-title' === $block['attrs']['className'] ) { $string_one = get_field('your_acf_field'); $string_two = get_field('your_other_acf_field'); $block_content = str_replace( '{{magic}}', $string_one . ' ' . $string_two, $block_content ); } return $block_content; }, 10, 2 );Update acf fields in the
get_fieldfunctions.January 19, 2023 at 11:15 am #2501999Ashia
Its working fine, and also I wanted to use more custom meta fields so I have edited the codes like this,,, Idk its ok or not.
$string_one = get_field(‘meta_001’);
$string_two = get_field(‘meta_002’);
$string_three = get_field(‘meta_003’);
$string_four = get_field(‘meta_004’);
$string_five = get_field(‘meta_005’);
$string_six = get_field(‘meta_006’);
$block_content = str_replace( ‘acf1’, $string_one . ‘ ‘ , $block_content );
$block_content = str_replace( ‘acf2’, $string_two . ‘ ‘ , $block_content );
$block_content = str_replace( ‘acf3’, $string_three . ‘ ‘ , $block_content );
$block_content = str_replace( ‘acf4’, $string_four . ‘ ‘ , $block_content );
$block_content = str_replace( ‘acf5’, $string_five . ‘ ‘ , $block_content );
$block_content = str_replace( ‘acf6’, $string_six . ‘ ‘ , $block_content );also in static text instead of {{magic}}, I am using acf1, acf2 and so on. after editing working fine.
January 19, 2023 at 5:56 pm #2502215Fernando Customer Support
Hi Ashia,
That should be alright. But a quicker process would be to run
str_replaceonce. So, something like this:add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && 'custom-post-title' === $block['attrs']['className'] ) { $string_one = get_field('meta_001'); $string_two = get_field('meta_002'); $string_three = get_field('meta_003'); $string_four = get_field('meta_004'); $string_five = get_field('meta_005'); $string_six = get_field('meta_006'); $block_content = str_replace( 'acf1', $string_one . ' ' . $string_two . ' ' . $string_three . ' ' . $string_four . ' ' . $string_five . ' ' . $string_six, $block_content ); } return $block_content; }, 10, 2 );January 19, 2023 at 9:13 pm #2502318Ashia
thanks a lot.
January 19, 2023 at 9:24 pm #2502332Fernando Customer Support
You’re welcome, Ashia!
-
AuthorPosts
- You must be logged in to reply to this topic.