[Resolved] Page Hero Button show if PHP

Home Forums Support Page Hero Button show if PHP

  • This topic has 3 replies, 2 voices, and was last updated 5 months ago by David.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #1293367

    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! 😀

    Customer Support

    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 ) {
            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!

    Customer Support

    Awesome – glad to hear that 🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.