通过分配给该类型的帖子的每个类别循环Wordpress自定义帖子类型


Loop through a Wordpress Custom Post Type by each category assigned to posts of that type

我有一个archive-custom_post_name.php,它输出自定义post类型的内容,结构如下:

<?php get_header(); ?>
  <?php if ( have_posts() ) : ?>
    <?php $post_type = $wp_query->get_queried_object(); ?>
    // HTML Section header here
    <?php get_template_part('loop', 'masonry'); ?>
  <?php endif ?>
<?php get_footer(); ?>

生成一个包含循环的模板部件文件:

<?php while ( have_posts() ) : the_post(); ?>
  <?php get_template_part( 'content', 'masonry' ) ?>
<?php endwhile; ?>

这将导致一个模板部分,该部分为循环中的每个内容定义HTML。

我需要修改文件archive-custom_post_name.php,以便它遍历自定义帖子类型的帖子使用的所有类别,为每个类别输出一个HTML节头,以及下面包含每个类别的所有帖子。

这里有三个类别,每个类别有三个帖子

// HTML Section header for cat one
// Post with cat one
// Post with cat one
// Post with cat one
// HTML Section header for cat two
// Post with cat two
// Post with cat two
// Post with cat two
// HTML Section header for cat three
// Post with cat three
// Post with cat three
// Post with cat three

正如我所提到的,如果可能的话,我更喜欢的方法是修改顶层文件archive-custom_post_name.php,而不是从模板层次结构的下面修改。

感谢任何帮助,我相当难住!

首先你需要得到所有的猫

http://codex.wordpress.org/Function_Reference/get_categories

每个(猫作为猫)

在这个循环中,

可以查询每个类别http://codex.wordpress.org/Template_Tags/get_posts然后用标题和内容标签正常显示内容