[Resolved] Displaying custom field in hook element

Home Forums Support [Resolved] Displaying custom field in hook element

Home Forums Support Displaying custom field in hook element

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1101959
    Ruth

    Hello, thank you for this brilliant theme.

    I’m using GeneratePress Premium with Gutenberg. I also have the Code Snippets plugin installed.

    I’m sorry to bother you with what I’m sure is a very basic query but I just can’t figure it out.

    I created a hook that displays a line of text followed by a button in the before_footer area of most pages and posts on my site (it’s a CTA pointing people to my contact page) – it looks great and works perfectly.

    Then I thought it would be even better if the line of text was different on every page because I could then replace the generic “Let me know how I can support your business” with something more specific to the page/post content.

    I did a bit of research and thought this was something I could achieve using a custom field.

    I’ve added a custom field called “cta-text” to several pages and posts and populated it.

    I understand that the next step is to add some code to php using Code Snippets, which will add the custom field to the theme. Then I’ll need to replace the existing static text in the hook element with another bit of code, which will pull the custom field text through from each page/post.

    I’m hoping you can tell me what code I need to put in each location. And, for the php code, do I literally just click on “Add New” in Code Snippets and paste it straight in the box or does it need to go somewhere specific? If so, how do I get it there?

    Forgive my coding ignorance! I’ve already used custom fields to great effect in my hero header and to add a subtitle in WP Show Posts but I can’t quite work this one out.

    My site is a total mess right now because I’m in the middle of redoing it, so I’ve got maintenance mode on, but let me know if you need to see it and I’ll make it live for a bit.

    #1101989
    David
    Staff
    Customer Support

    Hi there,

    you can add the following PHP directly to the Hook Element:

    <?php
    // Get cta-text custom field value
    $cta_text_value = get_post_meta( get_the_ID(), 'cta-text', true );
    // Check cta-text-value is not empty and return value
    if ( ! empty( $cta_text_value ) ) {
        $content = $cta_text_value;
    } else {
        // else return fall back message
        $content = 'Fallback message';
    }
    // Display contet
    echo '<p class="custom-cta-text">' . $content . '</p>';
    ?>

    You an edit the $content = 'Fallback message'; to whatever you like for when the cta-text is empty.

    #1102873
    Ruth

    Brilliant – thank you David – that was easier than I thought!

    #1103047
    David
    Staff
    Customer Support

    Happy to be of help

    #2514513
    Karmen

    Hello David,

    Thanks for that code, I could almost modify it to my needs. I do have a question, though. When my custom field is empty, I would like to not show anything at all. Right now, it shows my piece of code and the fallback message, but I need it to not show anything when the field is empty. Is it possible?

    #2514602
    Ying
    Staff
    Customer Support

    Hi Karmen,

    I modified the code below:

    <?php
    // Get cta-text custom field value
    $cta_text_value = get_post_meta( get_the_ID(), 'cta-text', true );
    // Check cta-text-value is not empty and return value
    if ( ! empty( $cta_text_value ) ) {
        $content = '<p class="custom-cta-text">' . $cta_text_value. '</p>';
    } else {
        // else return nothing
        $content = '';
    }
    // Display content
    echo $content;
    ?>
    #2514657
    Karmen

    Hello Ying,

    Thanks for your reply. The modified code still returns the HTML ‘<p class=”custom-cta-text”> </p>’ it’s just that there is no content between the p tags. I’d like the PHP code to return nothing at all, not even the p tags, when there is no value entered in the custom field. I don’t know if this is possible.

    #2514665
    Ying
    Staff
    Customer Support
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.