Archived topic
Dynamic Post Meta – Conditional Display?
5 replies · Started by Pete on March 3, 2022
For my player profiles, I am using a specific page hero block element – part of which displays post meta where the value of squad_number isn't 0. See example here: https://www.boroguide.co.uk/players/966/.
Is it possible, however, to have a condition where a squad_number of 99 won't display on the front end (as if it were a 0)?
The reason for using 99 for some player profiles is because it affects the ordering of the players on my 'Squad' page, which is to start on 1.
If I change instances of 99 to 0 and adjust the code accordingly, this works in so far as a squad number doesn't appear on their profile. But it also means those players are listed first on the 'Squad' page, whereas I wish for them to be listed last as an indication they are junior members of the team.
Hope that makes sense.
Hi Pete,
Give this PHP snippet a try:
add_filter( 'render_block', function( $block_content, $block ) {
if ( 'generateblocks/headline' === $block['blockName']) {
$block_content = str_replace('99', '', $block_content );
}
return $block_content;
}, 10, 2 );
Hi Ying,
Added that snippet to my functions.php file and it's done the trick in terms of removing the '99' squad numbers. But it's now also affecting the formatting of the "Apps" count in the red bar below: https://www.boroguide.co.uk/players/972/
What's causing this and is it something I can fix?
Hi there,
you can try this function instead:
add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
if ( ! empty( $block['attrs']['className'] ) && 'your-custom-class-name' === $block['attrs']['className'] ) {
$text = str_replace('99', '', $text );
}
return $text;
}, 10, 2 );
Note:
you need to change your-custom-class-name to your own custom class.
Then select the block displaying the dynamic data and give it that class in Advance > Additional CSS Class(es)
Got it! Thanks.
You're welcome