簡體   English   中英

使用get_post()時如何在wordpress的query.php中處理未定義的偏移量0

[英]How to address undefined offset 0 in wordpress's query.php when using get_post()

在wordpress中,我正在使用:

$posts=get_posts($args)
foreach($posts as $post){
   //do stuff
} 

但是,當我沒有帖子時

未定義的偏移量:在第2859行的C:\\ xampp \\ htdocs \\ wrDTC \\ wp-includes \\ query.php中為0

我嘗試使用if($posts){}但是通知仍然出現了。 除了關閉php中的錯誤消息之外,我該如何阻止此通知?

使用像這樣的東西......

  if(!empty($args) && count($args)>0){
    $posts=get_posts($args)
    foreach($posts as $post){
       //do stuff
    } 
    }

您不能簡單地使用$ posts作為循環中變量的名稱。

“$ posts”在wordpress查詢中用作全局,必須是數組。

如果您選擇為var $ posts命名,則此結果為未定義的偏移0,因為您的$ posts沒有偏移0.記住,您只需重置它。

嘗試在$ args中定義偏移量('offset'=> 1),而不是使用通常的{}嘗試使用鈎子和“endforeach;”

我在wordpress dev中的foreach語句結構如下:

<?php foreach($myPosts as $post) : setup_postdata($post) ?>

      <?php the_title(); ?>
      <?php the_content(); ?>
      // etc
      <?php endforeach; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM