Archived topic
Display custom meta field value in elements
1 reply · Started by Daniel on May 26, 2021
Hi there,
I would like to display the value of a custom meta box after each post content. I embedded the following code into my functions.php to display the value on each post and the values are displayed at the very top of each post, but I would like to show them after the main content of each post.
add_action('the_post', function($post, $query){
if ( is_single() && 'idea' == get_post_type()) {
$post_id = $post->ID;
//get voters
$voters = get_post_meta($post_id,'up-voters',true);
//only continue if voters exist
if(count($voters)){
//start output
echo '<ul class="voters-listing">';
?>
<p> Positiv </p>
<?php
//loop through voters
foreach($voters as $voter){
$user_id = $voter[0];
$vote_time = $voter[1];
$user_object = get_userdata($user_id);
$voter_name = $user_object->first_name.' '.$user_object->last_name;
$voter_edit_link = get_admin_url().'user-edit.php?user_id='.$user_id;
$vote_time_nice = date_i18n(get_option( 'date_format' ) .' '.get_option( 'time_format' ),$vote_time);
echo '<li>';
//do image
echo '<img class="idea-author-profile-image" src="'.esc_url(idea_push_get_user_avatar($user_id)).'" />';
//do text
echo '<a href="'.$voter_edit_link.'">'.$voter_name.'</a> <em>'.$vote_time_nice.'</em>';
echo '</li>';
}
echo '</ul>';
}
That's why I would like to ask you, if there is a possibility with GeneratePress to get the output of these custom field at the bottom of each post?
Thank you very much for your advice,
Daniel
Hi Daniel,
Can you try changing the hook being used from the_post to generate_after_content or generate_after_main_content?
Or you can refer to this hook visual guide to pick the hook you find more appropriate.