[Resolved] Page Hero Button show if PHP

Home Forums Support [Resolved] Page Hero Button show if PHP

Home Forums Support Page Hero Button show if PHP

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1293367
    Arttu

    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! πŸ˜€

    #1293410
    David
    Staff
    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 ) {
            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

    #1293436
    Arttu

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

    #1293442
    David
    Staff
    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.