2015-01-12 41 views
-1

我有下面的代碼片段:無效的參數提供給的foreach即使它是一個數組

$post = new WP_Query($args); 
foreach ($post->$posts as $gallery){ 
    //code in the foreach loop 
} 

的後返回數組。我做了一個print_r($post)來查看結果,$ posts仍然是一個數組。下面是陣列結果:

WP_Query Object ([query] => Array ([post_type] => gallery [posts_per_page] => [paged] => 0 [tax_query] => Array ([0] => Array ([taxonomy] => portfolio-category [field] => id [terms] => 16)) [meta_query] => Array ([0] => Array ([key] => bmt_sport_name [value] => baseball [compare] => ==))) [query_vars] => Array ([post_type] => gallery [posts_per_page] => 10 [paged] => 0 [tax_query] => Array ([0] => Array ([taxonomy] => portfolio-category [field] => id [terms] => 16)) [meta_query] => Array ([0] => Array ([key] => bmt_sport_name [value] => baseball [compare] => ==)) [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [menu_order] => [category__in] => Array () [category__not_in] => Array () [category__and] => Array () [post__in] => Array () [post__not_in] => Array () [tag__in] => Array () [tag__not_in] => Array () [tag__and] => Array () [tag_slug__in] => Array () [tag_slug__and] => Array () [post_parent__in] => Array () [post_parent__not_in] => Array () [author__in] => Array () [author__not_in] => Array () [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [taxonomy] => portfolio-category [term_id] => 16 [order] => DESC) [tax_query] => WP_Tax_Query Object ([queries] => Array ([0] => Array ([taxonomy] => portfolio-category [terms] => Array ([0] => 16) [include_children] => 1 [field] => id [operator] => IN)) [relation] => AND) [meta_query] => WP_Meta_Query Object ([queries] => Array ([0] => Array ([key] => bmt_sport_name [value] => baseball [compare] => ==)) [relation] => AND) [date_query] => [request] => SELECT SQL_CALC_FOUND_ROWS wp_bmt_2_posts.ID FROM wp_bmt_2_posts INNER JOIN wp_bmt_2_term_relationships ON (wp_bmt_2_posts.ID = wp_bmt_2_term_relationships.object_id) INNER JOIN wp_bmt_2_postmeta ON (wp_bmt_2_posts.ID = wp_bmt_2_postmeta.post_id) WHERE 1=1 AND (wp_bmt_2_term_relationships.term_taxonomy_id IN (108)) AND wp_bmt_2_posts.post_type = 'gallery' AND (wp_bmt_2_posts.post_status = 'publish' OR wp_bmt_2_posts.post_status = 'private') AND ((wp_bmt_2_postmeta.meta_key = 'bmt_sport_name' AND CAST(wp_bmt_2_postmeta.meta_value AS CHAR) = 'baseball')) GROUP BY wp_bmt_2_posts.ID ORDER BY wp_bmt_2_posts.post_date DESC LIMIT 0, 10 [posts] => Array ([0] => WP_Post Object ([ID] => 6840 [post_author] => 1 [post_date] => 2015-01-11 01:05:48 [post_date_gmt] => 2015-01-11 01:05:48 [post_content] => [post_title] => Test Football [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => test-football [to_ping] => [pinged] => [post_modified] => 2015-01-12 19:28:41 [post_modified_gmt] => 2015-01-12 19:28:41 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/bmt/northwestern-lehigh/?post_type=gallery&p=6840 [menu_order] => 0 [post_type] => gallery [post_mime_type] => [comment_count] => 0 [filter] => raw)) [post_count] => 1 [current_post] => -1 [in_the_loop] => [post] => WP_Post Object ([ID] => 6840 [post_author] => 1 [post_date] => 2015-01-11 01:05:48 [post_date_gmt] => 2015-01-11 01:05:48 [post_content] => [post_title] => Test Football [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => test-football [to_ping] => [pinged] => [post_modified] => 2015-01-12 19:28:41 [post_modified_gmt] => 2015-01-12 19:28:41 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/bmt/northwestern-lehigh/?post_type=gallery&p=6840 [menu_order] => 0 [post_type] => gallery [post_mime_type] => [comment_count] => 0 [filter] => raw) [comment_count] => 0 [current_comment] => -1 [found_posts] => 1 [max_num_pages] => 1 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => 1 [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => e6f988162f114f97ef1030324f7a7dd0 [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] =>) 

爲什麼我會收到此錯誤消息?

編輯:如果我做print_r($post->posts)我得到以下結果:

Array ([0] => WP_Post Object ([ID] => 6840 [post_author] => 1 [post_date] => 2015-01-11 01:05:48 [post_date_gmt] => 2015-01-11 01:05:48 [post_content] => [post_title] => Test Football [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => test-football [to_ping] => [pinged] => [post_modified] => 2015-01-12 19:28:41 [post_modified_gmt] => 2015-01-12 19:28:41 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/bmt/northwestern-lehigh/?post_type=gallery&p=6840 [menu_order] => 0 [post_type] => gallery [post_mime_type] => [comment_count] => 0 [filter] => raw)) 
+0

您是否也嘗試'print_r($ post - > $ posts)'因爲這是您正在使用的... – PeeHaa

+0

@PeeHaa我更新了這個問題,它顯示了我一個數組。 – user1048676

+1

不,它不。閱讀我的評論*和*您的代碼 – PeeHaa

回答

0

試着改變你的foreach線以下:

foreach ($post->posts as $gallery){ 

你在哪裏使用$post->$posts,因此PHP會首先確定值爲$posts(如果未定義,將爲null),並在$post對象中查看該值。

+0

這正是我如何設置的。沒關係。愚蠢的錯誤。 – user1048676

+0

@ user1048676您發佈的代碼與我發佈的代碼不一樣。 – T0xicCode