How can I display a custom meta value inside the post loop?
Asked on Sep 11, 2025
Answer
To display a custom meta value inside the WordPress post loop, you can use the `get_post_meta()` function. This function retrieves the value of a custom field for a specific post. You typically place this code within the loop in your theme's template files.
<!-- BEGIN COPY / PASTE -->
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$custom_value = get_post_meta( get_the_ID(), 'your_meta_key', true );
echo esc_html( $custom_value );
endwhile;
endif;
<!-- END COPY / PASTE -->Additional Comment:
- Replace "your_meta_key" with the actual key of your custom field.
- Ensure that the custom field is already added to your posts, otherwise the function will return an empty value.
- Use `esc_html()` to safely output the custom meta value, preventing XSS attacks.
- This code should be placed in a template file like `single.php` or `content.php` where the loop is executed.
Recommended Links: