Archived topic
Display custom fields in page
9 replies · Started by Oliver on March 12, 2023
I added a custom field in my post which contains custom schema.
e.g. <script type="application/ld+json">{"@context":"http://schema.org","...</script>
How do I show this specific custom field on every post?
Hi Oliver,
That would need custom development which would be out of our scope of support. It should be something like this: https://developer.wordpress.org/reference/hooks/wp_head/#:~:text=or%20for%20inline%20scripts%20which%20need%20to%20be%20placed%20in%20the%20head%2C
Just replace the script with a get_post_meta(YOUR-META-FIELD-NAME); add work your way through.
What you can do in GeneratePress is to create a Hook Element for each post and that schema script one by one.
Hi Fernando,
Thanks! but the issue with adding a hook element for each post is too much maintainability and I want to keep the custom schema on each edit post page.
Yes, you would need a custom code to hook your post meta field manually to the Head or somewhere you prefer.
This would be out of our scope, however. It would be good to ask for recommendations in our Facebook group as well: https://www.facebook.com/groups/1113359788719597
Here is the code but how do I add it after the
This seems to add it at the end of
function after_post_content($content){
if ( is_singular( 'post') ) {
$cf = get_post_meta( get_the_ID(), 'custom_schema', true );
if( ! empty( $cf ) ) {
$content .= $cf;
}
}
return $content;
}
add_filter( "the_content", "after_post_content" );
Sorry, where do you want to add this code exactly?
at the end of a post page, preferably after the site footer?
Preferably after this class.
Try using the wp_footer hook instead.
Reference: https://developer.wordpress.org/reference/hooks/wp_footer/
That's where scripts are usually placed.
that works thank you!
You're welcome, Oliver!