Hej,我会保持简短。我想在 WP 循环中输出这个:
Support
Category1
-Post1
-Post2
Category2
-PostA
-PostB
-PostC
Run Code Online (Sandbox Code Playgroud)
所以我想按位于自定义帖子类型中的类别订购帖子 - support(感谢 Types 插件创建,链接: ujeb.se/A4zqZ )。
我有这个:
<?php
$args = array('post_type' => 'support');
$query = new WP_Query($args);
while($query -> have_posts()) : $query -> the_post(); ?>
<p><?php the_category(); ?></p>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php the_content(); ?></p>
<?php endwhile; ?>
Run Code Online (Sandbox Code Playgroud)
我$query从我的自定义帖子类型 ( support) 中存储了所有必要的帖子,但我在按类别显示它们时遇到了问题。我相信我需要某种东西,foreach但我真的想不通。有什么建议?
/edit/
当前显示如下:
Support, Category1
Post1
---
Support, Category2
PostA
---
Support, Category1
Post2
etc.
Run Code Online (Sandbox Code Playgroud)
这是你如何做到的。您需要一个 foreach 循环来循环浏览类别。
<?php
$cats = get_categories();
foreach ($cats as $cat) {
$args = array(
'post_type' => 'support',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $cat->cat_ID,
),
),
);
$query = new WP_Query($args);
if ( $query->have_posts() ): ?>
<p><?php echo $cat->cat_name ; ?></p> <?
while($query -> have_posts()) : $query -> the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php the_content(); ?></p> <?php
endwhile;
endif;
// Added this now
wp_reset_query() ;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14965 次 |
| 最近记录: |