此自定义页面至少需要40秒才能加载。其他页面在3秒内加载完毕。我想在首页只显示今天的帖子,就像今天的新闻一样。代码有什么问题?
<div class="row">
<?php list($latest_post) = get_posts( array( "posts_per_page"=>1 ));
$date = substr($latest_post->post_date, 0,10); // Show only news from this date
?>
<div class="medium-8 large-8 columns" role="main">
<?php
unset($counter);
/*these are the category ID */
$cats = array(49, 10, 50);
#$cats = get_categories();
foreach($cats AS $cat):
$posts = get_posts( array( "posts_per_page"=>100, "category"=>$cat ));
$catname = get_the_category_by_ID($cat);
$foundPost = false;
foreach($posts AS $post):
$postdate = substr($post->post_date, 0,10);
if($postdate!=$date) break;
if(!$foundPost) { // Print title if at least one post is found
$foundPost = true;
if ( in_category('Category Text') ) {
echo '<div class="redBackground">'.$catname.'</div>';
}
}
?>
<?php
the_post();
?>
<div class="row">
<div class="small-2 columns"><?php the_post_thumbnail();?></div>
<div class="small-10 columns"> <div class="fontSize"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
<div class="contentTag"><?php the_tags(''); ?></div>
<div class="contentText"> <span style="color: #000"><?php echo people_Content(150); ?></span>
</div></div>
</div>
<?php
?>
<?php
endforeach;
endforeach; ?>
</div>
</div>
您需要运行一个更有选择性的查询,并且只选择您当天的帖子。下面是一个带有循环的查询,它应该带你到你需要去的地方:
//Get todays date
$today = getdate();
/*these are the category ID */
$cats = array(49, 10, 50);
//setup arguments for query including today's date
$args = array(
'category__in' => $cats,
'date_query' => array(
'year' => $today['year'],
'month' => $today['mon'],
'day' => $today['mday'],
)
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// this is where you will print out the details of each post
}
} else {
//There were no posts for today.
}
// Restore original Post Data (you may not need this but it can't hurt)
wp_reset_postdata();