在子类别页面上显示 wordpress 子类别帖子
Display wordpress sub-category posts on sub-category page
我正在尝试让 WP 功能正常工作。
我希望它显示所选子类别页面中的帖子,例如,当导航到:www.example.com/category/fruits/apples/ 时,显示 "apples" 类别下的所有自定义帖子。我想动态地执行此操作,以便无论子类别(苹果、橙子、梨等)的数量如何,每次访问子类别页面时都有效。
以下是我目前的功能,不知道get_query_var('cat')
是否实现正常。目前,当我访问子类别页面时,它会显示父类别 "fruits" 的所有帖子,但我希望它只显示 "apples" 帖子。
<?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(); ?>
将 $cat 变量添加到 query_posts 数组以获取当前选定的类别帖子。
$category = get_category( get_query_var( 'cat' ) );
$cat = $category->cat_name;
// only display product CPT
posts query_posts( array( 'post_type' => 'products', 'category_name' => $cat ) );
我正在尝试让 WP 功能正常工作。
我希望它显示所选子类别页面中的帖子,例如,当导航到:www.example.com/category/fruits/apples/ 时,显示 "apples" 类别下的所有自定义帖子。我想动态地执行此操作,以便无论子类别(苹果、橙子、梨等)的数量如何,每次访问子类别页面时都有效。
以下是我目前的功能,不知道get_query_var('cat')
是否实现正常。目前,当我访问子类别页面时,它会显示父类别 "fruits" 的所有帖子,但我希望它只显示 "apples" 帖子。
<?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(); ?>
将 $cat 变量添加到 query_posts 数组以获取当前选定的类别帖子。
$category = get_category( get_query_var( 'cat' ) );
$cat = $category->cat_name;
// only display product CPT
posts query_posts( array( 'post_type' => 'products', 'category_name' => $cat ) );