this is the correct solution
<?php $meta_cat_name = get_post_meta( $post->ID, 'related blog articles', true ); if( !empty( $meta_cat_name ) ): $my_query = new WP_Query( array( 'category_name' => $meta_cat_name, 'posts_per_page' => 5, ) ); if ( $my_query->have_posts() ): while ( $my_query->have_posts() ) : $my_query->the_post(); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a> <?php endwhile; endif; endif; wp_reset_query(); ?>
----- original request and old code below -----
As I have yet to find a tutorial that does what I need I am using the More Fields plugin so that I can add partially filled custom fields/meta boxes on the edit posts page.
These have the key already set so that the user only needs to enter a value - in this case a category name which will then display 5 posts from that cat.
This works fine unless the post is published without a value being entered. This causes the published post to also list the 5 most recent posts. I assume its because it has been instructed that something is going there but only has half the info.
So how do I make the chunk of code below only appear if a value has been entered?
thanks
James
<?php $my_query = new WP_Query(array(
'category_name' => get_post_meta($post->ID, 'related blog articles', true),
'posts_per_page' => 5,
)); if ($my_query->have_posts()): ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>