
[英]Move WordPress posts and related taxonomy to custom post type & taxonomy
[英]WordPress: Separate posts by Post Type on Taxonomy Archive
我想为我的自定义分类法创建一个自定义存档页面( taxonomy.php ),在该页面中,文章按列表显示,按帖子类型分组。
我有三种帖子类型:-
我也有两种自定义分类法:-
我如何最好地解决这个问题?
我已经在自定义页面模板上实现了类似的功能,在该模板中,我已按照“ 技术领域自定义分类”术语(下面的代码)将“ 指南自定义帖子类型”的特定类别进行了分组,但是我无法按照上述方法进行翻译。
<?php
foreach ( $technical_area_terms as $technical_area_term ) {
$member_group_query = new WP_Query( array(
'post_type' => 'guides',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'technical_area',
'field' => 'slug',
'terms' => array( $technical_area_term->slug ),
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'p1000-guides', 'guides'),
)
)
) );
?>
<h2><a href="../../../technical_area/<?php echo $technical_area_term->slug; ?>"><?php echo $technical_area_term->name; ?></a></h2> <!-- Technical Area Title -->
<?php
if ( $member_group_query->have_posts() ) : ?>
<table>
<tr>
<th>Title</th>
<th>Issue</th>
<th>Date</th>
</tr> <?php
while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?>
<tr>
<td><?php the_title( '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' , '</a>' ); ?></td>
<td></td>
<td><?php the_time( get_option( 'date_format' ) ); ?></td>
</tr>
<?php endwhile; ?>
</table>
<?php else: ?>
No content
<?php endif; ?>
首先让我们获取帖子
// vars
$posts = $wp_query->posts;
$posts_sorted = array();
然后,我们可以遍历它们,并使用数组中的不同post_types排序到一个新的多维数组中。 看起来像这样:
// loop and reorder
foreach($posts as $key => $obj) {
if (!array_key_exists($obj->ID, $posts_sorted))
$posts_sorted[$obj->post_type][] = $obj;
}
现在,我们只需要遍历新数组并输出要输出的内容。 这是我遍历的示例
<?php
foreach($posts_sorted as $post_type => $post_arr) : ?>
<div class="wrapper">
<h3><?= $post_type ?></h3>
<div class="card-group">
<?php
foreach($post_arr as $post ) {
var_dump($post);
}
?>
</div>
</div>
<?php endforeach; ?>
在此示例中,我只是var_dumping post对象,但是您可以根据需要使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.