自定义帖子类型为“projects”,分级分类为“projects\\u category”
projects
- big-corporates
-- company one
---- first project 1
---- second project 1
---- third project 1
-- company two
---- first project 2
---- second project 2
-- company three
---- first project 3
---- second project 3
---- third project 3
- small-businesses
-- company four
---- first project 4
---- etc...
-- company five
-- company six
下面是“大公司”下“projects\\u category”中列出的所有项目的列表,而我想将列表限制为“大公司”每个子类别中的一个项目。
<?php
global $post;
$args = array( \'post_type\' => \'projects\', \'projects_category\' => \'big-corporates\' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php #the_title(); ?><?php $terms = get_the_term_list( $post->ID, \'projects_category\' ); $terms = strip_tags( $terms ); echo $terms; ?></a></li>
<?php endforeach; ?>
我正试图获得如下列表:
company one (links to first project 1)
company two (links to first project 2)
company three (links to first project 3)
我该怎么做?
SO网友:Bainternet
我不确定这是不是最好的方法,但它确实有效:
//First get all child categories if \'big_corporates\'
$categories = $args = array(
\'parent\' => $big_corporates_ID,
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hide_empty\' => 1,
\'taxonomy\' => \'projects_category\'
);
//then loop over them and get the first post of each:
echo \'<ul>\';
foreach ($categories as $category) {
$projects = get_posts(array(\'numberposts\' => 1, \'post_type\' => \'projects\', \'projects_category\' => $category->slug));
foreach( $projects as $project ) {
setup_postdata($project);
?><li><?php echo $category->name; ?> - <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php
}
}
echo \'</ul>\';