【发布时间】: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