簡體   English   中英

訪問數組/對象中的值?

[英]Accessing Values in an Array / Object?

嘗試循環瀏覽一組類別並顯示每個類別中最新文章的標題。

  $feed_sources = array('goose-creek','sleepy-creek','fobr');
  foreach ($feed_sources as $feed) {
    $args = array('category_name' => $feed, 'posts_per_page' => 1);
    $show = get_posts($args); 
    print_r($show);

此代碼返回

Array ( [0] => WP_Post Object ( [ID] => 79 [post_author] => 1 [post_date] => 2015-03-19 08:58:40 [post_date_gmt] => 2015-03-19 09:58:40 [post_content] => 

但是用$ show [0] ['post_title'],$ show [0] [post_title]或$ show [0]->'post_title'訪問它沒有運氣

另外,有沒有一種簡單的方法來使該數組與基本主題功能(例如the_title();)一起工作? 內容(); 等等?

您應該將其重寫為如下所示:

$feed_sources = array('goose-creek','sleepy-creek','fobr');

foreach ($feed_sources as $feed) {
    $args = array('category_name' => $feed, 'posts_per_page' => 1);

    // we are creating new WP_Query object instead using get_posts() function 
    $shows = new WP_Query($args);

    $shows->the_post();
    // now you can use the_title() and the_content()
    the_title();
}

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM