【问题标题】:Error in foreach loop whilst grabbing Array items抓取数组项目时foreach循环出错
【发布时间】:2015-10-22 10:58:46
【问题描述】:

我正在尝试在此 WordPress 数组中的“guid”下找到 URL,但是仅 foreach 循环就证明了错误。

您会注意到我尝试在每个 foreach 循环中获取信息的两种方法。我没有同时运行这些,只是在foreach中粘贴了两次尝试提供示例。

$media_items = get_attachments_by_media_tags('media_tags='.$category->term_id.'');
foreach($media_items as $item) {
    echo $item['guid'];
    echo $item->guid;
}

$media_items = get_attachments_by_media_tags('media_tags='.$category->term_id.'');
$media_items = array();
foreach($media_items as $item) {
    echo $item['guid'];
    echo $item->guid;
}

整个循环:

<?php
//for each category, show all posts
$cat_args=array(
  'orderby' => 'id',
  'order' => 'DESC'
   );
$categories=get_categories($cat_args);
  foreach($categories as $category) {
    $args=array(
      'showposts' => -1,
      'category__in' => array($category->term_id),
      'caller_get_posts'=>1,
      'category__not_in' => array( 96, 67 ),
    );
    $media_items = get_attachments_by_media_tags('media_tags='.$category->term_id.'');
    foreach($media_items as $item) {
        echo $item['ID'];
        echo $item->ID;
    }

    $media_items = get_attachments_by_media_tags('media_tags='.$category->term_id.'');
    $media_items = array();
    foreach($media_items as $item) {
        echo $item['guid'];
        echo $item->guid;
    }

    echo '<pre>'; var_dump($media_items); echo '</pre>';
    $posts=get_posts($args);
      if ($posts) {
        echo '<h2>'. $category->name.'</h2> <br />';
        foreach($posts as $post) {
          setup_postdata($post); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        } // foreach($posts
      } // if ($posts
    } // foreach($categories
?>

var_dump($media_items);

NULL
array(1) {
  [0]=>
  object(WP_Post)#564 (24) {
    ["ID"]=>
    int(6074)
    ["post_author"]=>
    string(1) "4"
    ["post_date"]=>
    string(19) "2014-11-24 08:22:44"
    ["post_date_gmt"]=>
    string(19) "2014-11-24 08:22:44"
    ["post_content"]=>
    string(0) ""
    ["post_title"]=>
    string(47) "4255 Research Trends Issue 39 v2 singles online"
    ["post_excerpt"]=>
    string(0) ""
    ["post_status"]=>
    string(7) "inherit"
    ["comment_status"]=>
    string(4) "open"
    ["ping_status"]=>
    string(6) "closed"
    ["post_password"]=>
    string(0) ""
    ["post_name"]=>
    string(47) "4255-research-trends-issue-39-v2-singles-online"
    ["to_ping"]=>
    string(0) ""
    ["pinged"]=>
    string(0) ""
    ["post_modified"]=>
    string(19) "2015-10-22 09:18:35"
    ["post_modified_gmt"]=>
    string(19) "2015-10-22 09:18:35"
    ["post_content_filtered"]=>
    string(0) ""
    ["post_parent"]=>
    int(0)
    ["guid"]=>
    string(113) "http://researchtrends.mktdev.co.uk/wp-content/uploads/2014/11/4255-Research-Trends-Issue-39-v2-singles-online.pdf"
    ["menu_order"]=>
    int(0)
    ["post_type"]=>
    string(10) "attachment"
    ["post_mime_type"]=>
    string(15) "application/pdf"
    ["comment_count"]=>
    string(1) "0"
    ["filter"]=>
    string(3) "raw"
  }
}

使用时出错:

$media_items = get_attachments_by_media_tags('media_tags='.$category->term_id.'');
foreach($media_items as $item) {
echo $item['ID'];
}

Warning: Invalid argument supplied for foreach() in /home/jasdit5/researchtrends.mktdev.co.uk/wp-content/themes/researchtrends/article-archives.php on line 38
NULL

Fatal error: Cannot use object of type WP_Post as array in /home/jasdit5/researchtrends.mktdev.co.uk/wp-content/themes/researchtrends/article-archives.php on line 39

【问题讨论】:

    标签: php arrays wordpress foreach


    【解决方案1】:

    由于评论太长,我将其添加为答案。

    请在您的代码示例中标记与您发布的错误消息相对应的行。

    我可以从您的示例中说的是,在第二个循环中,您已经使用 $media_items = array(); 重置了您的数组,因此在之后的 foreach() 循环中它将为空。你能试试下面的测试代码并显示结果吗?

    $categories=get_categories($cat_args);
    foreach($categories as $category) {
        $args=array(
          'showposts' => -1,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1,
          'category__not_in' => array( 96, 67 ),
        );
    
        $media_items = get_attachments_by_media_tags('media_tags='.$category->term_id);
    
        var_dump($media_items);
    
        foreach($media_items as $item) {
            var_dump($item);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我想出了获取它的方法,记下我的 foreach 循环现在所在的位置。

      $media_items = get_attachments_by_media_tags('media_tags='.$category->term_id.'');
      foreach($media_items as $item) {
          if(!empty($item->guid)) {
              echo '<strong><a href="' . $item->guid . '">Download: ' . $category->name . ' PDF</a></strong>';
              echo '<br />';
              echo '<br />';
              }
          }
      

      整个循环:

      <?php
      //for each category, show all posts
      $cat_args=array(
        'orderby' => 'id',
        'order' => 'DESC'
         );
      $categories=get_categories($cat_args);
        foreach($categories as $category) {
          $args=array(
            'showposts' => -1,
            'category__in' => array($category->term_id),
            'caller_get_posts'=>1,
            'category__not_in' => array( 98, 66, 67 ),
          );
      
          $posts=get_posts($args);
            if ($posts) {
              echo '<h2>'. $category->name.'</h2> <br />';
              $media_items = get_attachments_by_media_tags('media_tags='.$category->term_id.'');
              foreach($media_items as $item) {
                  if(!empty($item->guid)) {
                      echo '<strong><a href="' . $item->guid . '">Download: ' . $category->name . ' PDF</a></strong>';
                      echo '<br />';
                      echo '<br />';
                      }
                  }
              foreach($posts as $post) {
                setup_postdata($post); ?>
                <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
                <?php
              } // foreach($posts
            } // if ($posts
          } // foreach($categories
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-20
        • 2012-07-05
        • 2018-03-30
        • 2018-07-15
        • 1970-01-01
        相关资源
        最近更新 更多