是否有一种简单的方法可以获取每个已注册的分类法,并且对于每个已注册的分类法,获取该分类法的所有术语,对于每个术语,获取post计数,而无需实际获取所有post数据?
我想这绝对是可能的。我还假设它需要使用一些非常长的数据库查询$wpdb
.
是否有一种简单的方法可以获取每个已注册的分类法,并且对于每个已注册的分类法,获取该分类法的所有术语,对于每个术语,获取post计数,而无需实际获取所有post数据?
我想这绝对是可能的。我还假设它需要使用一些非常长的数据库查询$wpdb
.
你只需要get_terms
- 这允许您从一个(或多个)分类中获取所有(或部分)术语。
默认情况下,它排除了“空”项,因此您需要适当地设置参数。
//Array of taxonomies to get terms for
$taxonomies = array(\'category\',\'post_tags\',\'my-tax\');
//Set arguments - don\'t \'hide\' empty terms.
$args = array(
\'hide_empty\' => 0
);
$terms = get_terms( $taxonomies, $args);
$empty_terms=array();
foreach( $terms as $term ){
if( 0 == $term->count )
$empty_terms[] = $term;
}
//$empty_terms contains terms which are empty.
如果希望以编程方式获取已注册分类法的数组,可以使用get_taxonomies()
<?php
// your taxonomy name
$tax = \'cat\';
// get the terms of taxonomy
$terms = get_terms( $tax, $args = array(
\'hide_empty\' => false, // do not hide empty terms
));
// loop through all terms
foreach( $terms as $term ) {
// Get the term link
$term_link = get_term_link( $term );
if( $term->count > 0 )
// display link to term archive
echo \'<a href="\' . esc_url( $term_link ) . \'">\' . $term->name .\'</a>\';
elseif( $term->count !== 0 )
// display name
echo \'\' . $term->name .\'\';
}
?>
我对wordpress还很陌生,每天都会遇到新的事情——其中一件就是今天我偶然遇到的get_terms 注意到它基本上与get_category. 使用其中一种有什么特别的原因吗?有什么我遗漏的吗?
是否有一种简单的方法可以获取每个已注册的分类法,并且对于每个已注册的分类法,获取该分类法的所有术语,对于每个术语,获取post计数,而无需实际获取所有post数据?
我想这绝对是可能的。我还假设它需要使用一些非常长的数据库查询$wpdb
.
你只需要get_terms
- 这允许您从一个(或多个)分类中获取所有(或部分)术语。
默认情况下,它排除了“空”项,因此您需要适当地设置参数。
//Array of taxonomies to get terms for
$taxonomies = array(\'category\',\'post_tags\',\'my-tax\');
//Set arguments - don\'t \'hide\' empty terms.
$args = array(
\'hide_empty\' => 0
);
$terms = get_terms( $taxonomies, $args);
$empty_terms=array();
foreach( $terms as $term ){
if( 0 == $term->count )
$empty_terms[] = $term;
}
//$empty_terms contains terms which are empty.
如果希望以编程方式获取已注册分类法的数组,可以使用get_taxonomies()
<?php
// your taxonomy name
$tax = \'cat\';
// get the terms of taxonomy
$terms = get_terms( $tax, $args = array(
\'hide_empty\' => false, // do not hide empty terms
));
// loop through all terms
foreach( $terms as $term ) {
// Get the term link
$term_link = get_term_link( $term );
if( $term->count > 0 )
// display link to term archive
echo \'<a href="\' . esc_url( $term_link ) . \'">\' . $term->name .\'</a>\';
elseif( $term->count !== 0 )
// display name
echo \'\' . $term->name .\'\';
}
?>
我正在做一个get_terms() 我试图按自定义术语元排序的查询。自定义术语元键是\'order\' 它是一个数值(介于1和10之间)。我尝试了以下方法,但顺序似乎没有遵循元值-任何指针都是值得赞赏的。$type_terms = get_terms( \'type\', array( \'hide_empty\' => false, array( \'key\' => \'order\', ), \'or