我有一个驱动程序帖子类型,它有一个叫做团队的关系。我想按团队分组驱动程序,以便我可以在他们的分组“团队”中将它们一起输出。希望这是有道理的。我真的不明白我试过的一些文件,但它似乎缺少细节。这就是我现在所拥有的:
<?php
$type = 'drivers';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 12,
'ignore_sticky_posts'=> 0,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'team-driver' );
endwhile;
?>
这是我的帖子类型。
答案 0 :(得分:1)
你可以试试这个
<?php
// get all the categories
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat)
{
// Get the cateogory ID
$cat_id= $cat->term_id;
//Header for the cateogry
echo "<h2>".$cat->name."</h2>";
// Make a custom wordpress query
query_posts("cat=$cat_id&post_per_page=12");
// start wordpress loop
if (have_posts()) : while (have_posts()) : the_post();
?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
<?php
echo '<hr/>';
endwhile;
endif; // End of WordPress loop
} // End of foreach loop through category
?>
如果您只想获得一个类别(驱动程序),那么
<?php
$cat_name = 'drivers';
$term = get_term_by('name', $cat_name, 'category');
$cat_id=$term->term_id;
query_posts("cat=$cat_id&post_per_page=12");
// start wordpress loop
if (have_posts()) : while (have_posts()) : the_post();
?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
<?php
echo '<hr/>';
endwhile;
endif;
?>
可能这个可以帮到你
<?php
$cat_names = array('team1', 'team2', 'team3');
foreach($cat_names as $cat)
{
$term = get_term_by('name', $cat, 'category');
$cat_id=$term->term_id;
query_posts("cat=$cat_id&post_per_page=12");
// start wordpress loop
if (have_posts()) : while (have_posts()) : the_post();
?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
<?php
echo '<hr/>';
endwhile;
endif;
}
?>