Wordpress/Wishlist会员,如果foreach其他循环
问题描述:
有一刻脑冻结。我有一个自定义的SQL查询工作,则下列代码显示在使用收藏会员会员网站的所有即将到来的帖子,根据会员的注册日期:Wordpress/Wishlist会员,如果foreach其他循环
$pageposts = $wpdb->get_results($querystr, OBJECT);
if ($pageposts) {
global $post;
foreach ($pageposts as $post) {
setup_postdata($post);
$postDate = strtotime($post->post_date);
$todaysDate = strtotime(now);
if ($postDate > $todaysDate) {
echo '<li>';
echo the_title();
echo '</li>';
}
else {
// Do nothing for now
}
}
}
变量$ pageposts总是被填充,但链接只显示如果将来有可用的页面向Wishlist的内容调度程序中的成员发送给成员。我的问题是 - 根据调度器,如果没有其他成员的帖子,我怎么回应一些效果'在foreach循环之外没有更多可用的帖子?已经搜查过,但没有什么比较适合我。谢谢。
答
$pageposts = $wpdb->get_results($querystr, OBJECT);
$counter = 0; // setup a counter with initial value = 0
if ($pageposts) {
global $post;
foreach ($pageposts as $post) {
setup_postdata($post);
$postDate = strtotime($post->post_date);
$todaysDate = strtotime(now);
if ($postDate > $todaysDate) {
echo '<li>';
echo the_title();
echo '</li>';
$counter++; // increment of $counter (+1)
}
}
}
if($counter == 0) { // if counter was not incremented, you know that there were no post
echo 'no posts!';
}
+0
非常感谢@steven。奇迹般有效 :) – user2590830
should not be'if(count($ pageposts) steven
使用一些标记并在您的第一个if条件中进行更改,比循环结束后可以检查相同的标记。 – Rikesh
或者在开始位置设置一个计数器= 0并在echo-block中做一个增量。最后,如果计数器为0,那么你可以输出你的消息 – steven