功能
function get_taxonomy_names($post_id, $term_name) {
$collections = wp_get_post_terms($post_id, $term_name, array("fields" => "all"));
$count = count($collections);
$return = \'\';
if($count > 0) {
$i = 1;
foreach($collections as $collection) {
$return .= $collection->name;
if($count > 1) {
if($count == $i) {
continue;
}
$return .= \', \';
}
$i++;
}
}
return $return;
}
产品模板
< $products_category = get_categories(array(\'taxonomy\'=>\'category-products\'));?>
<div class="sorting-block">
<div class="content-xs">
<ul class="sorting-nav sorting-nav-v1 text-center">
<? foreach ( $products_category as $collection ): ?>
<li class="filter" data-filter="category"><?=$collection->name;?></li>
<?endforeach;?>
</ul>
</div>
<? $products = get_posts( array(\'post_type\' => \'products\', \'posts_per_page\' => 400,) ); ?>
<? foreach ( $products as $post ) : setup_postdata( $post ); ?>
<? $pic = types_render_field("product-pic", array("url"=>"true")); ?>
<? $serial_number = types_render_field("serial-number", array("url"=>"true")); ?>
<ul class="row sorting-grid">
<li class="col-md-3 col-sm-6 col-xs-12 mix" data-cat="category">
<a href="#">
<img class="img-responsive" src="<?=$pic;?>" alt="">
<span class="sorting-cover">
<span><? the_title(); ?></span>
<p></p>
</span>
</a>
</li>
</ul>
<?endforeach;?>
问题
我有按类型插件“自定义分类法”创建的产品类别。我想在有人单击类别名称时按类别显示产品,该名称应仅显示注册到例如“acril”类别的产品。问题是,当我调试产品时,它并没有显示类别。因此,如果您注意到我在第1圈中有数据过滤器,在第二圈中有值为“category”的数据cat,我应该连接两个foreach圆圈。这应该是动态的,若post并没有显示任何键,那个么什么才是连接的关键。
SO网友:Arsalan
如果要显示特定类别中的帖子,还需要传递类别名称。。
只需在get\\u posts数组中再添加一个参数。
替换此
<? $products = get_posts( array(\'post_type\' => \'products\', \'posts_per_page\' => 400,) ); ?>
用这个
<? $products = get_posts( array(\'post_type\' => \'products\', \'category_name\' => \'category_name_here\', \'posts_per_page\' => 400,) ); ?>