Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do I list a custom field and custom taxonomies for each result in my loop below?

<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('post_type=customtype&showposts=10'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();

the_title(); // <- works
//list custom field here
//list custom taxonomy here

endwhile;

wp_pagenavi();              
?>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can get custom field values with get_post_meta. Like so...

<?php
$wp_query = new WP_Query();
$wp_query->query('post_type=customtype&showposts=10'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();

the_title(); // <- works
if( $some_meta = get_post_meta( get_the_ID(), 'the_meta_key_you_want', true )
{
    // you could put other formatting here.
    echo $some_meta; 
}

endwhile;

You'll need to change the_meta_key_you_want in the call to get_post_meta, of course.

For a custom taxonomy, you'll want to use get_the_terms or the_terms. the_terms is a bit more like the_tags or the_category, it spits out HTML for you. get_the_terms will just return an array (a list) of the term objects.

Here's an example of get_the_terms. The Codex entry for it also has some good examples. You'll have to change your_taxonomy, of course.

<?php
$wp_query = new WP_Query();
$wp_query->query('post_type=customtype&showposts=10'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();

the_title(); // <- works
$terms = get_the_terms( get_the_ID(), 'your_taxonomy' );
if( $terms && ! is_wp_error( $terms ) )
{
    foreach( $terms as $t )
    {
        echo '<a href="' . get_term_link( $t->slug, 'your_taxonomy' ) . '">' . esc_html( $t->name ) . '</a>';
    }
}

endwhile;
share|improve this answer
    
Awesome. The taxonomy one works. But why am I getting a white screen of death on the get_post_meta one. –  Dave Mar 8 '12 at 4:13
    
Try now. I didn't capitalize the ID property of $post. –  chrisguitarguy Mar 8 '12 at 4:21
    
Found out that you need to use "get_the_id()" instead of "$post->ID" in "get_post_meta( get_the_id(), 'the_meta_key_you_want', true );" –  Dave Mar 8 '12 at 4:23
    
... if you want to update your original post to include my last comment I would love to give you the points! Also, make sure you change "$term->name" to "$t->name". –  Dave Mar 8 '12 at 4:25
    
You shouldn't need to use get_the_ID, but it's changed. –  chrisguitarguy Mar 8 '12 at 4:28
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.