【发布时间】:2014-07-10 03:58:35
【问题描述】:
我可以列出我所有的自定义帖子类型类别和帖子,但是我只想列出当前类别的帖子。
<?php
$post_type = 'product';
$tax = 'product_category';
$tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC'));
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => - 1,
'orderby' => 'title',
'order' => 'ASC',
'caller_get_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
echo '<h3>' . $tax_term->name . '</h3>';
while ($my_query->have_posts()) : $my_query->the_post();
?>
<p><i class="fa fa-angle-right" style="margin-right:5px;"></i><?php the_title(); ?> </p>
<?php
endwhile;
}
wp_reset_query();
}
}
?>
这将列出我想要的自定义帖子类型中的所有类别,但会显示所有帖子,而不仅仅是与当前类别相关的帖子。
下面是我在网上找到的一些代码,它将显示活动类别的帖子。我试图将两者结合起来,但我缺乏 php 和条件循环意味着我似乎无法让它工作。
<?php
$tax = get_query_var('tax'); // get current category
$yourcat = get_category($cat);
// now get all 'business' posts that are in the current custom taxonomy
query_posts( array( 'post_type' => 'product', 'product_category' => $yourcat->slug ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<h3><?php the_title(); ?></h3>
<?php endwhile; endif; wp_reset_query(); ?>
希望有人可以帮助我,将两段代码合并或告诉我一个新选项。
例如,如果我在显示 DVD 结果的页面上,我想要一个显示其他类别的侧边栏,并在侧边栏中的 DVD 标题下方显示 DVD 类别中的标题(帖子)。
DVD
-标题
-TITLE2
-TITLE3
蓝光
光盘
【问题讨论】:
标签: php conditional-statements wordpress