【发布时间】:2014-01-09 13:25:27
【问题描述】:
我正在尝试制作一个自定义模板来显示来自相同自定义帖子类型但不同类别的多个循环。
这就是我所追求的:
来自自定义帖子类型:“作品集”
在自定义类别 1“音乐”中:
- 顶部有 1 个精选帖子
- 音乐标题
- 3 个子特色帖子
- 12 个帖子(仅标题)
在自定义类别 2“演讲者”中: - 主持人标题 - 3 个帖子
在自定义类别 3“新闻”中: - 新闻标题 - 3 个帖子
这是我正在使用的代码:
<?php if (have_posts()) : while (have_posts()) : the_post(); //WP loop ?>
<?php the_content(); ?>
<?php $args=array( //Loop 1
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'music',
'posts_per_page' => 16
);
$myloop = new WP_Query($args);
if($myloop->have_posts()) : while($myloop->have_posts()) :
$myloop->the_post();
?>
<!--the content -->
<?php endwhile; endif; ?>
<?php wp_reset_query(); // end music loop ?>
<h2>Presenters</h2>
<?php $args=array( //Loop 2
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'presenters',
'posts_per_page' => 3
);
$myloop = new WP_Query($args);
if($myloop->have_posts()) : while($myloop->have_posts()) :
$myloop->the_post();
?>
<!--the content -->
<?php endwhile; endif; ?>
<?php wp_reset_query(); // end presenters loop ?>
<h2>News</h2>
<?php $args=array( //Loop 3
'post_type' => 'dt_portfolio',
'taxonomy' => 'dt_portfolio_category',
'term' => 'news',
'posts_per_page' => 3
);
$myloop = new WP_Query($args);
if($myloop->have_posts()) : while($myloop->have_posts()) :
$myloop->the_post();
?>
<!--the content -->
<?php endwhile; endif; ?>
<?php wp_reset_query(); // end news loop ?>
<?php endwhile; endif; // end WP loop?>
总体而言,这 3 个循环运行良好。
我需要帮助的部分是第一个循环部分。我需要从同一个自定义分类“dt_portfolio_category”->“音乐”中获取所有 16 个帖子。但是将它们分成 1 个顶部精选帖子(全角),然后是标题,然后是 3 个子精选帖子(3 列),然后是 12 个只有标题的帖子(3 列)。我试图将它分成 3 个单独的循环,但是内容被重复了......我认为必须有一种更清洁的方法来做到这一点。
谢谢!
【问题讨论】:
-
作为评论:你有一个父“循环”,里面有 3 个子“循环”。如果不正确,这是无用的。剪掉父“循环”。更重要的是,您对 WP_query 和 wp_reset_query() 有点困惑; 1)您实例化了3个同名的WP_Query对象,这不是错误,但会使代码更难阅读;而不是
$myloop使用$music、$presenters和$news例如 2)wp_reset_query()重置全局$post和相关的主查询,但它不会重置从 WP_Query 类实例化的对象:它们不'不需要重置。 -
谢谢,可以稍微清理一下代码。
标签: wordpress loops custom-post-type custom-taxonomy no-duplicates