我用了tutorial由Alicia Ramirez提供设置Isotope Plugin对于 WordPress 页面。我的目标是为帖子提供简单的砖石布局以及过滤它们的选项。
通过本教程,我学习了如何为后端设置的所有类别生成过滤器,然后加载所有帖子。这是通过这段代码完成的:
<ul id="filters" class="cat-filter">
<li><a href="#" data-filter="*" class="selected">Alles</a></li>
<?php
$terms = get_terms("category"); // get all categories, but you can use any taxonomy
$count = count($terms); // How many are they?
if ( $count > 0 ){ // If there are more than 0 terms
foreach ( $terms as $term ) { // for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
我想知道如何才能仅生成过滤器并加载特定类别集(而不是所有类别)的帖子。我可以通过更改通过 $terms = get_terms("category");
获取所有类别的初始代码段来实现此目的吗?
最佳答案
get_terms
还接受参数数组,其中包含用于指定要返回哪些术语的参数。
您没有说明如何识别特定类别,因此我将在示例中使用术语名称,但您可以将其更改为按 slug, term id, meta_query or anything else that is allowed 进行搜索.
仅获取名为“新闻”和“事件”的术语的示例
$args = array( 'names' => array( "news", "events"));
$terms = get_terms("category", $args);
如果您想获取特定 ID 的术语,您可以使用以下内容: $args = array( 'include' => array( 1,4,7 ));//仅术语 ID 1,4 和 7
关于javascript - 同位素插件 : Only generating filters for specific categories?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45793505/