如何获得使用蛞蝓从类别职位?(How do I get posts from category us

2019-08-02 06:01发布

我有我自己的主题,我想从一个特定的类别显示在我的主页上的帖子。

到目前为止,我已经取得了这样的:

<?php
    global $post;
    $args = array( 'numberposts' => 10, 'category' => 6 );
    $posts = get_posts( $args );
    foreach( $posts as $post ): setup_postdata($post); 
?>

    <divs with the_title() the_excerpt() etc ></div>

<?php 
    endforeach; 
?>

但是,如果我要得到什么用的蛞蝓种类? 或者它可以简单地从管理面板中进行类别选择框?

Answer 1:

更换你category与参数category_name

<?php
    global $post;
    $args = array( 'numberposts' => 10, 'category_name' => 'cat-slug' );
    $posts = get_posts( $args );
    foreach( $posts as $post ): setup_postdata($post); 
?>

<divs with the_title() the_excerpt() etc ></div>

<?php endforeach; ?>

欲了解更多信息: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters



Answer 2:

假设你有类别名称“冰蛋糕”和类别蛞蝓为“冰蛋糕”,那么我们的代码来检索后根据类别“冰蛋糕”如下:

<?php
              $args = array( 'posts_per_page' => 3,
               'category_name' => 'ice-cakes' );

              $icecakes = get_posts( $args );
              foreach ( $icecakes as $post ) : setup_postdata( $post ); ?>
                  <li>
                      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                  </li>
              <?php endforeach; 
              wp_reset_postdata(); ?>


文章来源: How do I get posts from category using the slug?
标签: wordpress