如何在WordPress中做一个组? [英] How to do a group by in the Wordpress?
本文介绍了如何在WordPress中做一个组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个司机职位类型,有一个叫团队的关系。我想要团队将车手分组,这样我就可以在他们的车队中一起输出。希望这是有道理的。我真的不明白我有一些尝试过的文档,但它似乎缺少细节。这是我现在所拥有的:
I have a drivers post type which has a relationship called team. I want to group drivers by team so I can output them together in their groupings 'teams'. Hopefully that makes sense. I don't really understand the documentation I have a tried a few things but it seems to be missing details. This is what I have at the moment:
<?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;
?>
这是我的发布类型。
Here is my post type.
推荐答案
您可以试试这个
<?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
?>
如果您只想获得一个类别(司机),那么
If you want to get only one category (drivers) then
<?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;
?>
可能是这个可以帮助你的人
May be this one can help you
<?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;
}
?>
这篇关于如何在WordPress中做一个组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文