WP Query Foreach有无限循环


WP Query Foreach has infinite loop

我需要帮助。我不明白为什么这个脚本会无限循环,因为我的网站现在一直冻结服务器,即限制资源到关闭我的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?>

如何解决这个问题?由于

global $post;之后请添加以下

$old_post = $post; 

然后在endforeach之后添加以下

wp_reset_postdata()

之后加上

 $post = $old_post;

希望能有所帮助

<?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;

        }
?>

你的foreach循环没有正确关闭,这就是它无限重复的原因。

将代码的最后一行替换为:

<?php endforeach; ?>