在Wordpress中,我如何在分类页面上列出由帖子所在子类别划分的自定义帖子类型?
我的例子:我有自定义分类法广告。我也有一个自定义的帖子类型叫做advertising。
类别结构:
- 页脚
- 栏
- 赞助商页面
- 黄金银青铜
如果我去myurl.com/sponsors-page/,它将显示所有的广告在金,银和铜类别。到目前为止一切顺利。但我想让它按子类别的顺序显示它们并回显子类别名称。例如:
- 金
- 广告1 广告2
- 银
- 广告3
- 广告4
- 铜
- 广告5
- 广告6
我如何做到这一点?请随意质疑我的方法,我是Wordpress新手。
我觉得这可能是一个重复,但相信我,当我说我已经尝试搜索。
我也刚开始使用Wordpress,但我认为你应该这样做。
// get available taxonomies
$taxonomies = get_object_taxonomies ( (object) array ('post_type' => 'subcategory' ));
// loop all taxonomies
foreach( $taxonomies as $taxonomy ) {
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
// loop through the terms
foreach( $terms AS $term ) {
// get posts
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug" );
// check for posts
if ( $posts-> have_posts() ) {
// how your header (gold,silver,bronze)
echo '<h2>' . $term-> name . '</h2>';
// loop through posts
while ( $posts-> have_posts() ) {
// get the post
$posts-> the_post();
// show your ad
echo $posts-> post-> post_content;
// Update temporary value
$posts_count++;
}
}
}
}