您只需使用WP_Query
. 您只需确保您使用的是自定义分类法还是内置类别。有关参考,请参阅:Is There a Difference Between Taxonomies and Categories?
下面是一个自定义分类的示例(这也可以用于内置类别,只需更改\'taxonomy\' => \'MY_CUSTOM_TAXONOMY\',
到\'taxonomy\' => \'category\',
):
$args = array(
\'post_type\' => \'MY_CUSTOM_POST_TYPE\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'DESC\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'MY_CUSTOM_TAXONOMY\',
\'field\' => \'slug\',
\'terms\' => \'THE_SLUG_FROM_MY_TERM\',
),
),
);
$the_query = new WP_Query( $args );
// The Loop
echo \'<ul>\';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo \'<li>\' . get_the_title() . \'</li>\';
}
echo \'</ul>\';
/* Restore original Post Data */
wp_reset_postdata();
对于单个术语,还可以直接使用查询变量,因此可以更改
$args
像这样的事情
$args = array(
\'post_type\' => \'MY_CUSTOM_POST_TYPE\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'DESC\',
\'MY_CUSTOM_TAXONOMY\' => \'THE_SLUG_FROM_MY_TERM\',
);
如果您有内置类别,请更改
$args
至以下内容
$args = array(
\'post_type\' => \'MY_CUSTOM_POST_TYPE\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'DESC\',
\'cat\' => \'THE_ID_OF_YOUR_CATEGORY\',
);
有关更有用的参数及其用法,请访问
WP_Query
在法典中