0
我试图让WP功能正常工作。在子类别页面上显示wordpress子类别帖子
我希望它显示来自所选子类别页面的帖子,例如,当导航到:www.example.com/category/fruits/apples/时,显示“苹果”类别下的所有自定义帖子。我想动态地做到这一点,所以无论子类别(苹果,橙子,梨等)的数量如何,每次访问子类别页面时都会起作用。
以下是我目前的功能,但我不知道get_query_var('cat')
是否正确实施。目前,当我访问子类别页面时,它会显示父类别为“fruits”的所有帖子,但我希望它仅显示“苹果”帖子。
<?php
$cat = get_query_var('cat'); // get current category
$yourcat = get_category($cat);
// only display product CPT posts
query_posts(array('post_type' => 'products'));
if (have_posts()) : while (have_posts()) : the_post();
?>
<div class="col-sm-3">
<div class="thumbnail">
<div class="more"><a href="<?php the_permalink(); ?>"><span class="fa fa-location-arrow"></span></a></div>
<?php the_post_thumbnail(); ?>
<div class="caption">
<a href="<?php the_permalink(); ?>" class="btn btn-default" role="button"><?php the_title(); ?></a>
</div>
</div>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
我想我已经设法解决了这个问题!我对查询帖子并不熟悉,但在查看了这些代码后,发现了我正在寻找的内容。这是更新的代码位: '$ category = get_category(get_query_var('cat')); $ cat = $ category-> cat_name; //只显示产品CPT帖子 query_posts(array('post_type'=>'products','category_name'=> $ cat));' –
干得好。将调整后的代码放入答案中,并将其标记为已解决。这将使人们更容易找出未解决的问题。 – RST