如何显示特定类别的wordpress帖子

时间:2015-02-11 22:18:20

标签: wordpress posts

我的帖子显示在我网站主页的底部。我想限制它,以便只显示某个类别中的帖子。有没有办法让它按类别ID显示?

这是我的代码:

<?php $args = array( 'post_type' => 'post', 'posts_per_page' => 4 );
                $loop = new WP_Query( $args ); ?>
            <?php if ($loop->have_posts()) : ?>
            <ul class="clearfix">
                <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <li>
                    <article class="clearfix">
                        <figure>
                            <a href="<?php the_permalink(); ?>">
                            <?php if ( the_post_thumbnail('mlab-thumb-list-view-img') ) {
                                the_post_thumbnail('mlab-thumb-list-view-img');
                            } ?>
                            </a>
                        </figure>
                        <div class="figure-details">
                            <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
                            <span><?php echo get_the_date('F j, Y'); ?></span>
                            <a href="<?php the_permalink(); ?>" class="read-more"> read more <i class="icon-rightarrow"></i></a>
                        </div>
                    </article>
                </li>
                <?php endwhile; ?>
            </ul>

3 个答案:

答案 0 :(得分:0)

是的,您将它添加到代码顶部的$ args数组中。将catnumber替换为类别的id。

$args = array( 'post_type' => 'post', 'posts_per_page' => 4, 'cat' => catnumber );

答案 1 :(得分:0)

you can use the below code
   <?php
   $cat = 44
    $args = array(
        'orderby' => 'date',
        'order' => 'DESC',
        'post_type' => 'post',
        'numberposts' => 8,
         'category__in' => array($cat)
      );
    $posts=get_posts($args);
 if ($posts) {
  foreach($posts as $post) {
   setup_postdata($post);
 ?>
  <p><?php the_time('m.d.y') ?> <a href="<?php the_permalink() ?>"     rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php }
 }
}
?>

答案 2 :(得分:0)

以下两种方法可以按特定类别发布。

方法-1

$args = array(
    'post_type' => 'post', // Enter your post type name
    'cat' => 2, // Enter your cat ID
    'orderby' => 'post_date',
    'order' => 'DESC',
    'posts_per_page' => 4,
    'post_status' => 'publish'
);

方法-2

$args = array(
    'post_type' => 'post', // Enter your post type name
    'orderby' => 'post_date',
    'order' => 'DESC',
    'posts_per_page' => 4,
    'post_status' => 'publish'
    'tax_query' => array(
        array(
            'taxonomy' => 'my_custom_taxonomy', // Enter your taxonomy name
            'terms' => 7, // Enter your cat ID
            'include_children' => true // Remove if you need posts from term 7 child terms 
        )
    ),
    // Rest of your arguments
);