Php 如何按类别列出帖子摘要并保持主题格式?

Php 如何按类别列出帖子摘要并保持主题格式?,php,wordpress,Php,Wordpress,下面的代码列出了所有类别中的最后一篇文章,并使用当前主题格式化摘要 使用get\u categories()、WP\u Query()和几个循环应该足够简单 // Get all your categories $categories = get_categories( array( 'parent' => 0, /* we only want parent categories */ 'orderby' => 'name', /* order by categor

下面的代码列出了所有类别中的最后一篇文章,并使用当前主题格式化摘要


使用
get\u categories()
WP\u Query()
和几个循环应该足够简单

// Get all your categories
$categories = get_categories( array(
    'parent' => 0, /* we only want parent categories */
    'orderby' => 'name', /* order by category name, ascending */
    'order'   => 'ASC'
) );

// Loop through each one
foreach( $categories as $category ) {

    ?><h2><?php echo( $category->name ); ?></h2><?php

    // Get some posts from each one
    $query = new WP_Query( array( 'category_name' => $category->name ) );

    // Do all the stuff your theme already does    
    ?>

        <div class="posts-loop">
        <?php
           while ($query->have_posts()) {
              $query->the_post();
        ?>
        <?php
        /* Include the Post-Format-specific template for the content.
         * If you want to override this in a child theme, then include a file
         * called content-___.php (where ___ is the Post Format name) and that will be used instead.
         */
        get_template_part('template-parts/' . $post_template);
        ?>

        <?php
           }
        ?>
        </div><!-- / .posts-loop -->

    <?php

    // Reset query and loop again to next category, if there is one
    wp_reset_query();
}
//获取所有类别
$categories=get_categories(数组(
“parent”=>0,/*我们只需要父类别*/
'orderby'=>'name',/*按类别名称排序,升序*/
“订单”=>“ASC”
) );
//循环通过每一个
foreach($categories作为$category){
?>

哇!谢谢。成功了!我用头撞了墙好几个小时想弄明白。