Site logo

Archived topic

Page Hero Button show if PHP

3 replies · Started by Arttu on May 20, 2020

Viewing posts 1–4 of 4

I am trying to create a page hero that displays a button ONLY IF this custom field has content: custom_field.hero-button-text

This is my code:
<a class="button" href="{{custom_field.hero-button-url}}">{{custom_field.hero-button-text}}</a>

I guess it requires a PHP snippet and little modification to the code.

ps. I was quite pessimistic about generatepress but bought the premium anyway. After few days of testing it out, I have to say I am really impressed. Not even mad about missing the birthday promotion! :D

Hi there,

glad to hear you're enjoying the theme :)

I think the easiest solution would be to create a shortcode instead of using the template tag. Try this PHP snippet:

add_shortcode( 'hero-button', function() {
    
    // Get your url and label
    $url = get_post_meta( get_the_ID(), 'hero-button-url', true );
    $label = get_post_meta( get_the_ID(), 'hero-button-text', true );
    
    // Output Button if label is complete
    
    if ( $label ) {
        ob_start();
        printf('<a class="button" href="%1$s">%2$s</a>',esc_url( $url ),$label);
	return ob_get_clean();
    }
});

Then add [hero-button] to your hero

Thanks a lot for the quick reply. Code works perfectly!

Awesome - glad to hear that :)

This archived topic is closed to new replies.