Archived topic
Adding custom fields to hook element
9 replies · Started by Margaret on October 7, 2021
Hello,
I am trying to add a "summary" paragraph to the top of each blog post.
I created a custom field called "summary," inserted the paragraph, and saved the post.
I created a hook with the following code:
<div class="blog_post_summary">
<p>
<b>Summary: </b>{{custom_field.summary}}
</p>
</div>
Under settings, I set to "generate_before_content." I enabled "Execute Shortcodes" and "Execute PHP."
Under Display Rules, I set to display on all Posts.
However, on the actual post, it is displaying as "Summary: {{custom_field.summary}}" and is not populating the custom field.
Thanks,
Margaret
Hi Margaret,
{{custom_field.summary}} is a pattern used for template tags which only works on Header Elements.
For Hook elements, you'll have to enable "Execute PHP" and fetch the field using PHP codes.
Example:
<div class="blog_post_summary">
<p>
<b>Summary: </b><?php get_post_meta(get_the_ID(),'summary'); ?>
</p>
</div>
Hi Elvin, thanks so much for your help. I added that code to the Hook element and made sure "Execute PHP" is enabled.
It is displaying as just "Summary: " but the Value I entered in the Post Custom Fields isn't displaying.
Hi there,
try changing:
<?php get_post_meta(get_the_ID(),'summary'); ?>
to:
<?php get_post_meta( get_the_ID(),'summary', true ); ?>
Hi David, thanks for your help. I updated the PHP code as you suggested above but I'm still getting the same result. At the top of the post, it says "Summary: " but the Value from the Custom Field isn't displaying.
Best,
Margaret
Could we get temporary admin access to the site ?
Yes, credentials are in the private information below. Thanks so much.
Try this instead:
<?php $summary = get_post_meta( get_the_ID(),'summary', true ); ?>
<div class="blog_post_summary">
<p><b>Summary: </b><?php echo $summary; ?></p>
</div>
That worked perfectly! Thanks for all the help.
Glad to hear that!