【问题标题】:WordPress: How to list all subcategories' first post in one page?WordPress:如何在一页中列出所有子类别的第一篇文章?
【发布时间】:2012-10-20 16:49:57
【问题描述】:

我有一个类别有 600 个子类别, 有没有办法用每个子类别的一篇文章列出所有子类别?

  • sub-1 最新帖子标题
  • sub-2 最新帖子标题
  • sub-3 最新帖子标题
  • sub-4 最新帖子标题
  • sub-5 最新帖子标题
  • sub-6 最新帖子标题
  • sub-7 最新帖子标题
  • 分8个最新帖子标题

【问题讨论】:

    标签: wordpress categories posts


    【解决方案1】:

    为了检索所有子类别,请使用get_categories()

    例子:

    $args = array(
    'type'                     => 'post',
    'parent'                   => 'your_parent_category_id',
    'orderby'                  => 'name',
    'order'                    => 'ASC' );
    
    $your_categories = get_categories( $args );
    

    要获取帖子,请遍历结果并使用get_posts()

    $args = array(
        'numberposts'     => 1,
        'offset'          => 0,
        'category'        => your_subcategories,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_type'       => 'post',
        'post_status'     => 'publish' );
    
    $your_posts = get_posts( $args );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多