Archived topic
function to show custom field
5 replies · Started by yannis2810 on August 8, 2018
Hi,
my knowledge of php is non existent, so please be patient.
I need your help with a function that will show a custom field inside the content in single posts only.
I have found this code from your forum:
function db_after_single_content($content) {
if ( is_single() ) {
$aftercontent = 'this appears after the content';
$fullcontent = $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'db_after_single_content');
How can I show the custom field 'wpcf-linkurl' there where it says "this appears after the content" in the code above?
Regards,
Yannis
Hi there,
try this, as it includes querying the custom field exists - just swap out your_custom_field
function db_cf_content_filter( $content ) {
global $post;
if( is_single() && $meta = get_post_meta( $post->ID, 'your_custom_field', true ) ) {
$output = '<p>' . $meta . '</p>';
return $content . $output;
}
return $content;
}
add_filter( 'the_content', 'db_cf_content_filter', 10 );
Dear David,
the function works great. How can I format the custom field?
For example: <p>Visit the website: 'your_custom_field'</p>
Yannis
Hi there,
i edited the code above :)
Thanks!
Glad to be of help