- This topic has 5 replies, 2 voices, and was last updated 3 years, 7 months ago by
Fernando.
Viewing 6 posts - 1 through 6 (of 6 total)
-
AuthorPosts
-
September 19, 2022 at 8:06 pm #2348014
Fergal
Hey there,
I’d like to add a colored background to posts that have a meta value of 1 for ‘enhanced_listing’ custom field. I’ve done the following which almost works, but it results in every container for the post having the class “my-enhanced-jo”. Can you please help me fix this?
I’ve added class: “my-enhanced-bg” to the container containing a grid (please see imgur link)
Added this php:
add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'my-enhanced-bg' ) !== false ) { $my_meta = get_post_meta(get_the_ID(), 'enhanced_listing', true); if( $my_meta == 1) { $my_new_class = 'my-enhanced-jo'; $myreplace1 = 'class="'; $myinsert1 = 'class=" ' . $my_new_class . ' '; $block_content = str_replace( $myreplace1, $myinsert1 , $block_content ); } } return $block_content; }, 10, 2 );And using this CSS
CSS:
.my-enhanced-jo {
background: rgba(126,33,42,0.38);
}September 19, 2022 at 8:11 pm #2348015Fernando Customer Support
Hi Fergal,
Can you provide the link to the page in question here as well?
September 19, 2022 at 8:14 pm #2348018Fergal
Hey again Fernando,
Yes sorry I’ve provided it now as well as an image of a post where this is occurring.
Thanks!
September 19, 2022 at 8:26 pm #2348027Fernando Customer Support
Can you try this?:
add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'my-enhanced-bg' ) !== false ) { $my_meta = get_post_meta(get_the_ID(), 'enhanced_listing', true); if( $my_meta == 1) { $my_new_class = 'my-enhanced-jo'; $myreplace1 = 'my-enhanced-bg'; $myinsert1 = $myreplace1 . ' ' . $my_new_class; $block_content = str_replace( $myreplace1, $myinsert1 , $block_content ); } } return $block_content; }, 10, 2 );September 19, 2022 at 8:32 pm #2348031Fergal
Worked perfectly thank you Fernando!
September 19, 2022 at 8:33 pm #2348032Fernando Customer Support
You’re welcome Fergal!
-
AuthorPosts
Viewing 6 posts - 1 through 6 (of 6 total)
- You must be logged in to reply to this topic.