Php WP查询Foreach具有无限循环

Php WP查询Foreach具有无限循环,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我需要帮助。我不明白为什么这个脚本会无限循环,因为我的站点现在一直冻结服务器,也就是说,将资源限制到关闭SQL server的程度 global $post; $args = array( 'posts_per_page' => 4, 'numberposts' => 4, 'offset' => 0, 'category' => $id, 'orderby' => '

我需要帮助。我不明白为什么这个脚本会无限循环,因为我的站点现在一直冻结服务器,也就是说,将资源限制到关闭SQL server的程度

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;
$args=数组(
“每页帖子数”=>4,
“numberposts”=>4,
“偏移量”=>0,
“类别”=>$id,
'orderby'=>'post_date',
“订单”=>“描述”,
“元密钥”=>“”,
“meta_值”=>“”,
“post_type”=>“post”,
“post\u mime\u type'=>”,
“post_parent'=>”,
“发布状态”=>“发布”,
“抑制_过滤器”=>true
);
$tips=get_posts($args);
foreach($tips as$post):设置_postdata($post);?>

我如何解决这个问题?谢谢

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

将代码的最后一行替换为以下内容:

<?php endforeach; ?>

全球$post之后请添加以下内容

$old_post = $post; 
wp_reset_postdata()
然后在
endforeach
之后添加以下内容

$old_post = $post; 
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;


        }
?>

Hi@Anant,我已经添加了输出。Pliz忽略$i=1,将其删除。谢谢,让我调整一下,当然谢谢,希望这能修复所有的语义错误!错误的输出,似乎仍然有效,但查询中的无限循环问题仍然存在,我没有语法错误,只是查询限制了我的服务器资源;死亡
将其放在
$tips=get_posts($args)之后和checkThank@Tarun,实际代码正在关闭。我只是在这里犯了错误!我没有语法错误,只是查询似乎在无限循环,它限制了我的服务器资源。我以前没有见过这个,希望它解决了我的难题。谢谢这不会给我任何输出,但是如果这种情况持续下去,我可能会切换到while循环。正如您在while循环中看到的,您应该只在链接中包装帖子缩略图,请确保已附加帖子缩略图。或者在图像后添加
链接标签。我用同样的颂歌在主页上显示最新的博客文章