有一瞬间大脑冻结。我有以下代码使用自定义SQL查询,根据成员的加入日期显示运行Wishlist Member的会员网站上的所有即将发布的帖子:
$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循环之外回应“没有其他帖子可用”的效果?已搜索过,但没有什么能与我匹敌。感谢。
答案 0 :(得分:0)
$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!';
}