Site logo

Archived topic

Display a block conditionally

4 replies · Started by Rekindle on September 13, 2021

Viewing posts 1–5 of 5

Hi,

On a specific page, I display the user's leaderboard rank using a shortcode. This block is bang in the middle of the page (see screenshot). The rank is generated by a shortcode.

Now I only want to display this rank if it's < 10. Is there a way to do this?

Some options I've considered after searching the forum:

1. Creating another shortcode that runs the rank generating shortcode and only displays the rank if it's < 10. This seems a big ugly.

2. I could set up the shortcode block as an Element and I could use the generate_block_element_display hook to hide the block. The problem is that this block is in the middle of the post. So I can't figure out a way to place this block in the middle of the post
I could work around that by creating one Element with this shortcode block, and another Element with all other blocks that follow after it. But this seems hard to maintain.

3. I could register a hook inside a shortcode and place that shortcode in the middle of the post (https://generatepress.com/forums/topic/hook/)

I was wondering if you have any simple ways of doing this. Thanks!

Hi there,

option #2 + #3:

You basically create a hook in the middle of the page content. You can do it by adding a portable hook through a shortcode. You can use my PHP snippet here - https://generatepress.com/forums/topic/inserting-custom-php-html-into-gutenberg-or-site-container/#post-1713422

You then do option #2 on the portable hook you've just placed.

But this is viable if you're only going to do this 1 time because you'll have to repeat this again and again if this is for re-occurring posts.

Option #1 shares an idea with #2 where you filter the display using generate_element_display.

But if I may suggest, if the shortcode is custom as well, you may as well edit that directly.

Else, I think option #1 will be the most practical because it has the least amount of steps to do because you're basically just wrapping the original shortcode with a helper shortcode so you can use PHP on it for the purpose of using conditions.

Thanks Elvin.

I tried #1 earlier as well but I was running into issues running one shortcode inside another.

function ShowTop20Rank() {
	$output = do_shortcode("[gamipress_leaderboard_user_position id="7594" current_user="yes" text="{position}" not_ranked_text="-"]");
	return $output;
}
add_shortcode('showtop20rank', 'ShowTop20Rank');

What am I doing wrong?

Got it. In case it might help someone else:

[showtop20rank][gamipress_leaderboard_user_position id="7594" current_user="yes" text="{position}" not_ranked_text="-"] [/showtop20rank]

function ShowTop20Rank($attr, $content) {
	$output = do_shortcode($content);
	if ($output < 20) {
		return $output;
	}
	else return NULL;
}
add_shortcode('showtop20rank', 'ShowTop20Rank');

Thanks again Elvin!

Your first attempt didnt have a condition so it didn't work. :D

Glad you got it sorted. Thanks for sharing it with us. :D

This archived topic is closed to new replies.