简体   繁体   中英

Error in foreach loop whilst grabbing Array items

I am trying to get the URL found under "guid" within this WordPress array, however the foreach loop alone is proving back an error.

You'll notice two method I've tried to try get the information out within each foreach loop. I did not run these at the same time, just pasted in two attempts within the foreach to provide examples.

$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;
}

Whole Loop:

<?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"
  }
}

Error when using:

$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

As this is too long for a comment, I'll add it as an answer.

Please mark the lines in your code sample which correspond to the error messages you have posted.

What I can say from your sample is that in the second loop you have resetted your array with $media_items = array(); so in the foreach() loop right afterwards it will be empty. Can you please try the following test code and display the results?

$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);
    }
}

I figured the method to grab it, take note of the location my foreach loop is now in too.

$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 />';
        }
    }

Whole loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM