- This topic has 7 replies, 2 voices, and was last updated 1 year, 8 months ago by Ying.
-
AuthorPosts
-
December 12, 2019 at 4:11 am #1101959Ruth
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.
December 12, 2019 at 4:55 am #1101989DavidStaffCustomer SupportHi 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.December 13, 2019 at 12:36 am #1102873RuthBrilliant – thank you David – that was easier than I thought!
December 13, 2019 at 3:54 am #1103047DavidStaffCustomer SupportHappy to be of help
January 30, 2023 at 10:53 am #2514513KarmenHello 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?
January 30, 2023 at 12:19 pm #2514602YingStaffCustomer SupportHi 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; ?>
January 30, 2023 at 1:04 pm #2514657KarmenHello 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.
January 30, 2023 at 1:09 pm #2514665YingStaffCustomer SupportOh right, sorry I’ve made another change here:
https://generatepress.com/forums/topic/displaying-custom-field-in-hook-element/#post-2514602 -
AuthorPosts
- You must be logged in to reply to this topic.