[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 4 posts - 1 through 4 (of 4 total)
  • Author
  • #1101959

    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.

    Customer Support

    Hi there,

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

    // 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.


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

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