Site logo

Archived topic

Adding a 'new' label to my posts

8 replies · Started by Christophe on August 13, 2021

Viewing posts 1–9 of 9

Hello GP team,

I'm using both GeneratePress Pro and GenerateBlocks Pro.

I created a block content template in Elements (with the featured image, categories, and title). Everything works very fine. I now would like to add some conditional PHP code to this block to display a 'new' label for my most recent posts. I already have the code, but I can't figure out how to make it work. Could you please guide me on how/where I should insert it?

Thank you,

--
Christophe

Hi Christophe,

I'm not sure if the solution you have in mind would work in the content template but what is the code?

Does it work out of the box without a content template?

Thanks, Leo for getting back to me!

I should be able to make it work with a function + inserting a PHP code. I used it before with my previous Avada theme, and that was working correctly.

The function (in my functions.php child theme file) looks like this:

function is_old_post($days = 5) {
	$days = (int) $days;
	$offset = $days*60*60*24;
	if ( get_post_time() > date('U') - $offset )
		return true;
	
	return false;
}

And here is the PHP code I need to insert (somewhere...):

if (is_old_post(5) ) { ?>
    <div class="new">NEW</div>

Any idea Leo?

Thank you!

Leo? Anyone from the GP team? I am kinda stuck here with that...

Another way to understand my question/issue: how can I associate a PHP script to a specific block I created?

Thank you again for your help on this matter!

I did a bit of research and I don't believe it's possible to execute PHP using a WordPress block, at least not without some extensive custom coding.

The workaround solution I can think of is to create a shortcode for it:
https://docs.generatepress.com/article/creating-a-shortcode/

Then add the shortcode with a shortcode block. Not sure if it will work either but it might be worth a shot.

You could also try posting an in WordPress general forum like this and see if anyone has a solution:
https://wordpress.stackexchange.com/

It's not a theme-specific question so hopefully some bright mind would chime in :)

Hi Leo,

Just to follow up on this thread. I tried your shortcode suggestion, unfortunately, it didn't work.

I found an interesting discussion in the Generate Blocks support, with the following comment/advice:

Try the generate_after_do_template_part hook - in the hook element you may need to select the Custom Hook first

It almost worked, but I only got it displayed outside of the container/block...

I leave this thread open (and not resolved yet) if you or someone think of a solution.

Thank you!

Hi there,

you should be able to convert that into a simple shortcode like so:

function is_old_post($html) {
    $days = 5;
    $offset = $days*60*60*24;
    $html = '';
    if ( get_post_time() > date('U') - $offset ) {
        $html = '<div class="new">NEW</div>';
    }
    return $html;
}

add_shortcode('post_age_label', 'is_old_post');

Then just add the shortcode [post_age_label] to a Headline Block.

WOW! That works like a charm... thank you so much, David!

Awesome - glad to hear that!

This archived topic is closed to new replies.