我需要帮助。我无法弄清楚为什么这个脚本无限循环b'se我的网站现在一直冻结服务器,即将资源约束到关闭我的SQL服务器。
global $post;
$args = array(
'posts_per_page' => 4,
'numberposts' => 4,
'offset' => 0,
'category' => $id,
'orderby' => 'post_date',
'order' => 'DESC',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$tips = get_posts( $args );
foreach( $tips as $post ) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
<?php if(has_post_thumbnail()){ the_post_thumbnail( 'medium', array('itemprop'=>'image') ); } ?>
</a>
<?php endforeach?>
我该如何解决这个问题?感谢
答案 0 :(得分:1)
在global $post;
之后,请添加以下内容
$old_post = $post;
然后在endforeach
添加以下
wp_reset_postdata()
然后添加
$post = $old_post;
希望这有帮助
答案 1 :(得分:1)
<?php
$args = array(
'posts_per_page' => 4,
'numberposts' => 4,
'offset' => 0,
'category' => $id,
'orderby' => 'post_date',
'order' => 'DESC',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$the_query1 = new WP_Query( $args );
if (count($the_query1->posts)>0) {
while ( $the_query1->have_posts() ) : $the_query1->the_post(); ?>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
<?php if(has_post_thumbnail()){ the_post_thumbnail( 'medium', array('itemprop'=>'image') ); } ?>
</a>
<?php endwhile;
}
?>
答案 2 :(得分:0)
你的foreach循环未正确关闭,这就是它无限重复的原因。
用以下代码替换代码的最后一行:
<?php endforeach; ?>