0

I loop through posts and by category_name=news, How can I display each post's custom field? thank you in advance.

<?php $preNewsPosts = new WP_Query();
$preNewsPosts->query('posts_per_page=3&category_name=news');
while($preNewsPosts->have_posts()): $preNewsPosts->the_post();
?>
<a href="<?php the_permalink(); ?>" class="atitle" ><?php the_post_thumbnail('thumbnail'); ?><?php the_title(); ?></a>
<?php
the_excerpt();
?>
<hr style="width: 95%; border: 1px solid #e6e6e6; " />
<?php
endwhile;
?>

1 Answer 1

1

This should work, just place inside the while loop. Make sure you change custom_field to the name of your custom field

echo get_post_meta($preNewsPosts->post->ID, 'custom_field', true);
Sign up to request clarification or add additional context in comments.

3 Comments

you might want to add global $post; as well
since your are using wp_query you can/should get id by using $preNewsPosts->post->ID.. no need for global $post
Big thanks to all! I put the echo get_post_meta($preNewsPosts->post->ID, 'location', true); right after excerpt() but it didn't work. And I have numbers of custom fields I want to display, can I loop through it without specifying the custom fields name?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.