我在wordpress中设置了一个名为" Portfolio"的自定义帖子类型。然后,自定义帖子类型设置了两个分类。他们被命名为“portfolio_categories'和' portfolio_sector'。
目前,我正在使用以下代码收集我的所有' portfolio_categories'并存储它们,以便它们可以作为CSS类输出以用于过滤。
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "portfolio_categories" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
然后将术语输出为类的代码如下:
<div class="<?php echo $termsString; ?>">
Content goes here
</div>
我如何编辑代码以存储分类法&#39; portfolio_sector&#39;并将它们作为类输出?
答案 0 :(得分:1)
您应该在您的上下文中获取其他人的术语数据
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "portfolio_categories" ); //Get the terms for this particular item#
$termsSectors = get_the_terms( $post->ID, "portfolio_sector" );
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsSectors as $term ) { // for each term
$termsSector .= $term->slug.' '; //create a string that has all the slugs
}
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
现在您可以通过var $termsSector
回显这个。
但是,作为提示,您还可以使用get_the_term_list()
来获取包含html的列表。也许这对你来说更容易,并且没有必要循环使用这些术语的数组。