I’m building a simple sidebar widget to display a loop of a custom post type “shows”.
Each “show” has about 3 custom fields, which i’d like to output via the loop as well. This is the code i’m using:
This is the loop within my plugin code:
<?php
// WIDGET CODE GOES HERE
$args = array( 'post_type' => 'shows', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$month = get_post_meta($post->ID,'month-abbreviation',true);
$date = get_post_meta($post->ID,'date',true);
$citystate = get_post_meta($post->ID,'city-state',true); ?>
<article class="sidebar-show clearfix">
<a class="show-link" href="<?php the_permalink(); ?>">
<div class="date-box">
<span class="month"><?php echo $month; ?></span>
<span class="date"><?php echo $date; ?></span>
</div>
<div class="venue-box">
<?php echo "<h4>".get_the_title()."</h4>"; ?>
<?php echo "<p>".$citystate."</p>"; ?>
</div>
</a>
</article>
<?php endwhile;
wp_reset_query();
wp_reset_postdata();
?>
<?php
echo $after_widget;
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("ShowsSidebarWidget");') );?>
This code pulls in the post titles, but doesn’t display the custom fields month-abbreviation, date, and city-state.
What’s missing here?
EDIT: Removed double quotes after avexdesign’s reply.
are you sure $post->ID is available in this context?
maybe you should try: get_the_ID() instead.
Ok, I see a few things you can try.
month-abbreviation – should be single quotes. Like ‘month-abbreviation’
Try taking out:
$month=
Â$date=
and$citystate =
Might need
echo
So Try:
echo get_post_meta($post->ID,'month-abbreviation', true);
echo get_post_meta($post->ID,'date', true);
echo get_post_meta($post->ID,'city-state', true);
Try something like:
<?php $loop = new WP_Query( array( 'post_type' => 'shows', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<span class="month"><?php echo get_post_meta($post->ID, 'month-abbreviation', true); ?></span>
<span class="date"><?php echo get_post_meta($post->ID, 'date', true); ?></span>
and so on… for your other custom IDs