Arrays 循环后数组的随机结果
首先,我花了一周多的时间从很多不同的来源把这段代码放在一起,如果你愿意的话,这是一个名副其实的弗兰肯斯坦。我不擅长编写PHP,但到目前为止已经做得不错了。如有任何意见,将不胜感激 我正在做一个项目,在这个项目中,我需要从cat 4的所有子类别中获取最新的10个结果,然后我希望随机显示结果。我已经看到了许多使用shuffle()的例子功能,但在正确执行时遇到问题 这是我的密码:Arrays 循环后数组的随机结果,arrays,wordpress,loops,random,shuffle,Arrays,Wordpress,Loops,Random,Shuffle,首先,我花了一周多的时间从很多不同的来源把这段代码放在一起,如果你愿意的话,这是一个名副其实的弗兰肯斯坦。我不擅长编写PHP,但到目前为止已经做得不错了。如有任何意见,将不胜感激 我正在做一个项目,在这个项目中,我需要从cat 4的所有子类别中获取最新的10个结果,然后我希望随机显示结果。我已经看到了许多使用shuffle()的例子功能,但在正确执行时遇到问题 这是我的密码: <?php $categories = get_categories( 'child_of=4' ); fo
<?php
$categories = get_categories( 'child_of=4' );
foreach($categories as $category) {
$args=array(
'showposts' => 10,
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=get_posts($args);
shuffle($posts);
if ($posts) {
foreach($posts as $post) {
setup_postdata($post); ?>
<div <?php post_class('boxy');?>>
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
<?php the_content(''); ?>
</div>
<?php
}
}
}
?>
此处链接到我的结果:
这段代码对每个类别中的结果进行随机化,但按类别显示。。
我希望我已经足够清楚,似乎是一个简单的解决办法
谢谢你需要先用你所有的分类来制作所有帖子的数组。比你需要洗牌一样。试试这个
$posts = array();
$categories = get_categories( 'child_of=4' );
foreach($categories as $category) {
$args=array(
'showposts' => 10,
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts = $posts + get_posts($args);
} // Close your foreach here
..比您的代码本身
$categories = get_categories( 'child_of=4' );
$cats = array();
foreach($categories as $category) {
$cats[] = $category->term_id;
}
$args=array(
'showposts' => 10,
'category__in' => $cats,
'orderby' => 'rand',
'caller_get_posts'=>1
);
$posts=get_posts($args);
我希望这将是一个更有效的解决办法。我还没有测试过 工作起来很有魅力,只需将“showpost”的数量改为更高的数字,因为只显示10个结果。嗯,我添加了一个新的儿童猫,它应该显示这个代码,我很难让它显示。我删掉了的子项,并能够通过专门针对post类型的cat id显示它们,但在尝试之后,我仍然无法理解为什么新的cat不会与其他cat一起显示。。