抓取Array项时foreach循环出错


Error in foreach loop whilst grabbing Array items

我试图在这个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

由于注释太长,我将把它作为答案添加。

请在你的代码样本中标记与你所发布的错误信息相对应的行。

我可以从你的样本中说的是,在第二个循环中,你已经用$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);
    }
}

我找到了抓取它的方法,请注意我的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
?>