I'm using this code to display related post by category and it works fine if there is only one category. But in the case of multiple categories I only want to use one, the first one. How do I limit this to just one category?
function related_posts_categories() {
$categories = get_the_category();
if ($categories) {
foreach ($categories as $category) {
$cat = $category->cat_ID;
$args=array(
'cat' => $cat,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page'=>4,
'ignore_sticky_posts'=>1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<div id="related_posts"><h3>Related Content</h3><ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li>
<a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
<?php the_post_thumbnail( 'related-posts' ); ?>
</a>
<div class="related_content">
<a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</div>
</li>
<?php
endwhile;
}
echo '</ul></div>';
}} wp_reset_query(); }