同位素插件:只为特定类别生成过滤器?

Isotope Plugin: Only generating filters for specific categories?

我为 Wordpress 页面使用了 tutorial provided by Alicia Ramirez to set up the Isotope Plugin。我的目标是为帖子设计一个简单的砌体布局 + 过滤它们的选项。

通过本教程,我学会了如何为后端设置的所有类别生成过滤器并随后加载所有帖子。这是用这段代码完成的:

<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 搜索。

示例仅获取名为 "news" 和 "events" 的术语

$args = array( 'names' => array( "news", "events")); 
$terms = get_terms("category", $args);

如果您想获取特定 ID 的条款,您可以使用类似以下内容的内容: $args = array( 'include' => array( 1,4,7 )); // 仅术语 ID 1,4 和 7

参考:All parameters allowed in $args