我正在尝试在" guid"下找到网址。在这个WordPress数组中,然而单独的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
答案 0 :(得分:0)
由于评论太长,我会将其添加为答案。
请在代码示例中标记与您发布的错误消息相对应的行。
我可以从您的示例中说明,在第二个循环中,您已使用$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);
}
}
答案 1 :(得分:0)
我想方法抓住它,记下我的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
?>