我已经注册了一个自定义帖子类型;“动物”;还有两种自定义分类法(“脊椎动物”和“类型”)分配给它,有自己的特定术语,用于;脊椎动物“;它将是“;哺乳动物“;和;爬行动物“;,“和”;“类型”;它将是“;“水类型”;和;接地类型;。
通过使用page。php我想显示所有自定义帖子(动物)的列表,这些帖子按照分配给“脊椎动物”自定义分类法的术语进行排序,在这里,我使用以下代码成功:
<?php
$post_type = \'animals\';
$tax = \'vertebrate\';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args=array(
\'post_type\' => $post_type,
"$tax" => $tax_term->slug,
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'caller_get_posts\'=> 1,
\'orderby\' => \'title\',
\'order\' => \'ASC\'
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo \'<h2>\'. $tax_term->name .\'</h2>\';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query();
}
}
?>
这给了我类似的东西:哺乳动物:牛;狗;海豚;虎鲸;爬行动物:蜥蜴;海龟;但是,我也想在前面的列表中显示;“类型”;通过在帖子链接后简单地返回其名称,每个帖子都被分配到了分类法(脊椎动物分类法中的术语除外)。
我正在努力实现这样的目标:
哺乳动物:
- 奶牛(地面型)
- 狗(地面型)
- 海豚(水型)
- 虎鲸(水型)
- 爬行动物:
- 蜥蜴(地面型)
- 海龟(水型)
- 我完全被困于此,而且我对WordPress和PHP的知识几乎为零,所以我很乐意接受任何帮助这件事。
更新我的朋友帮我解决了这个问题。它应该与此配合使用:
<?php $post_type = \'animals\'; $tax = \'vertebrate\'; $tax_terms = get_terms($tax); if ($tax_terms) { foreach ($tax_terms as $tax_term) { $args=array( \'post_type\' => $post_type, "$tax" => $tax_term->slug, \'post_status\' => \'publish\', \'posts_per_page\' => -1, \'caller_get_posts\'=> 1, \'orderby\' => \'title\', \'order\' => \'ASC\' ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { echo \'<h2>\'. $tax_term->name .\'</h2>\'; while ($my_query->have_posts()) : $my_query->the_post(); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> <?php setup_postdata( $post ); $terms = wp_get_post_terms( $post->ID, \'type\'); echo \'<span>\'. ($terms[0]->name) .\'</span></p>\'; ?> <?php endwhile; } wp_reset_query(); } } ?>