Post formats 实际上是taxonomy post_format
. 我写了一篇关于什么是分类法及其层次结构的帖子,你可以查看一下here
获取分类法下所有术语的列表post_format
, 简单使用get_terms
和var_dump()
结果
$terms = get_terms(\'post_format\',\'hide_empty=0\');
?><pre><?php var_dump($terms); ?></pre><?php
这将打印
array(5) {
[0]=>
object(stdClass)#207 (9) {
["term_id"]=>
string(3) "142"
["name"]=>
string(5) "Aside"
["slug"]=>
string(17) "post-format-aside"
["term_group"]=>
string(1) "0"
["term_taxonomy_id"]=>
string(3) "142"
["taxonomy"]=>
string(11) "post_format"
["description"]=>
string(0) ""
["parent"]=>
string(1) "0"
["count"]=>
string(1) "0"
}
[1]=>
object(stdClass)#310 (9) {
["term_id"]=>
string(3) "129"
["name"]=>
string(5) "Audio"
["slug"]=>
string(17) "post-format-audio"
["term_group"]=>
string(1) "0"
["term_taxonomy_id"]=>
string(3) "129"
["taxonomy"]=>
string(11) "post_format"
["description"]=>
string(0) ""
["parent"]=>
string(1) "0"
["count"]=>
string(1) "0"
}
[2]=>
object(stdClass)#309 (9) {
["term_id"]=>
string(3) "105"
["name"]=>
string(7) "Gallery"
["slug"]=>
string(19) "post-format-gallery"
["term_group"]=>
string(1) "0"
["term_taxonomy_id"]=>
string(3) "105"
["taxonomy"]=>
string(11) "post_format"
["description"]=>
string(0) ""
["parent"]=>
string(1) "0"
["count"]=>
string(1) "0"
}
[3]=>
object(stdClass)#308 (9) {
["term_id"]=>
string(3) "128"
["name"]=>
string(5) "Quote"
["slug"]=>
string(17) "post-format-quote"
["term_group"]=>
string(1) "0"
["term_taxonomy_id"]=>
string(3) "128"
["taxonomy"]=>
string(11) "post_format"
["description"]=>
string(0) ""
["parent"]=>
string(1) "0"
["count"]=>
string(1) "0"
}
[4]=>
object(stdClass)#307 (9) {
["term_id"]=>
string(3) "106"
["name"]=>
string(5) "Video"
["slug"]=>
string(17) "post-format-video"
["term_group"]=>
string(1) "0"
["term_taxonomy_id"]=>
string(3) "106"
["taxonomy"]=>
string(11) "post_format"
["description"]=>
string(0) ""
["parent"]=>
string(1) "0"
["count"]=>
string(1) "0"
}
}
有一件事我想强调一下,
standard
是
not 一种令人失望的帖子格式。任何没有指定帖子格式的帖子都应该被指定一个默认的帖子格式。问题是,如果您只需要没有特殊帖子格式的帖子,那么需要运行
tax_query
并排除所有具有帖子格式的帖子。
因此,要从特定的帖子格式获取帖子,可以运行tax_query
具有WP_Query
从video post格式获取所有帖子的示例:
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'terms\' => \'post-format-video\',
),
),
);
$query = new WP_Query( $args );
如果您需要获取没有附加任何帖子格式的帖子,您需要执行以下操作
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'terms\' => array(\'post-format-video\',\' post-format-quote\', \'post-format-gallery\', \'post-format-audio\', \'post-format-aside\' ),
\'operator\' => \'NOT IN\',
),
),
);
$query = new WP_Query( $args );