按类别划分的相关帖子,但只有一个

时间:2016-04-28 20:06:33

标签: wordpress related-content

我正在使用此代码按类别显示相关帖子,如果只有一个类别,它可以正常工作。但在多个类别的情况下,我只想使用一个,第一个。我如何将其限制为一个类别?

 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(); } 

0 个答案:

没有答案