2014-10-02 64 views
0

我想返回一個字符串,我可以在一個函數中使用(編程方式在WordPress中添加條款)。麻煩返回可用的字符串或數組與foreach循環

我產生我的字符串函數基本上是通過符合特定標準,是HTML的meta標籤循環如下:

function getYouTubeTags($post_id) { 

    $video_id = get_post_meta(get_the_ID(), 'rfvi_video_id', true); 
    $tag_url = "http://www.youtube.com/watch?v=" . $video_id; 
    $sites_html = file_get_contents($tag_url); 
    $html = new DOMDocument(); 
    @$html->loadHTML($sites_html); 
    $meta_og_tag = null; 

    foreach($html->getElementsByTagName('meta') as $meta) { 
     if($meta->getAttribute('property')==='og:video:tag'){ 
      $meta_og_tag = $meta->getAttribute('content'); 
      print_r ($meta_og_tag . ","); 
     } 
    } 

} 

當我簡單地執行這個(getYouTubeTags();),它返回字符串:

supra vs lambo,tt lambo,twin turbo,street race,texas streets,underground racing,supra,turbo supra,1200hp,nitrous,superleggera,gallardo, 

在我的功能項添加到一個帖子,下面不工作:

function rct_save_post_terms($post_id) { 

    $terms = getYouTubeTags(); 
    wp_set_post_terms($post_id, $terms, 'post_tag', true); 

} 

如果我手動添加字符串作爲從第一功能輸出,它的工作:

function rct_save_post_terms($post_id) { 

    $terms = 'supra vs lambo,tt lambo,twin turbo,street race,texas streets,underground racing,supra,turbo supra,1200hp,nitrous,superleggera,gallardo,'; 
    wp_set_post_terms($post_id, $terms, 'post_tag', true); 

} 

此外,根據WordPress的,$termswp_set_post_terms可以是一個數組或逗號分隔的字符串。

我知道我必須在這裏錯過簡單的東西,但似乎無法弄清楚。預先感謝您的幫助!

回答

2

既然你想獲得那些字符串被重用,爲什麼不回到那些:

function getYouTubeTags($post_id) { 
    $out = null; 

    $video_id = get_post_meta(get_the_ID(), 'rfvi_video_id', true); 
    $tag_url = "http://www.youtube.com/watch?v=" . $video_id; 
    $sites_html = file_get_contents($tag_url); 
    $html = new DOMDocument(); 
    @$html->loadHTML($sites_html); 
    $meta_og_tag = null; 

    foreach($html->getElementsByTagName('meta') as $meta) { 
     if($meta->getAttribute('property')==='og:video:tag'){ 
      // i seriously doubt this checking i think this should be 
      // if($meta->getAttribute('property') == 'og:video') { 
      $meta_og_tag = $meta->getAttribute('content'); 
      // print_r ($meta_og_tag . ","); 
      $out[] = $meta_og_tag; // gather them inside first 
     } 
    } 

    return implode(',', $out); // return implode comma delimited strings 
} 

然後utimately,那麼你可以使用它們:

function rct_save_post_terms($post_id) { 

    $terms = getYouTubeTags(); // strings are in here 
    wp_set_post_terms($post_id, $terms, 'post_tag', true); 

} 
+1

這工作完美。經過一段時間的努力之後,你不知道我是多麼的欣賞。謝謝! – 2014-10-02 01:09:00

+0

sure @JoeConlin im很高興這個答案幫了你 – Ghost 2014-10-02 01:11:16

+0

@Ghost的一個簡單問題:雖然這個工作完美,但它似乎有一個問題同時運行一個多個帖子。我相信這會耗盡記憶。有沒有什麼我可以添加到最後,所以每次執行時,它都會從前次執行中刷新內存? – 2014-10-02 01:24:02

1

您似乎沒有在原始函數中返回一個值。你需要使用;

return $meta_og_tag; 

在你的函數結束時返回一個值返回到指定變量。

此外,您需要使用.=將字符串追加到返回變量的末尾;

$meta_og_tag .= $meta->getAttribute('content'); 

OR可以節省每個屬性在返回數組和implode;

// inside loop 
$meta_og_tag[] = $meta->getAttribute('content'); 

// outside loop 
return implode(', ',$meta_og_tag); 

print_r只會回顯變量的內容,不會返回值。

希望這會有所幫助。

+0

我試圖用,但只有它返回FIRST值。我認爲停止循環的回報? – 2014-10-02 00:58:54

+0

你應該在循環之外和之後放置'return'語句。您還需要使用'。='將下一個值添加到字符串中,而不是將其替換。 – worldofjr 2014-10-02 01:01:56

+0

而不是在foreach循環中使用print_r,並知道返回$ meta_og_tag;只返回第一個值,也許有人可以讓我知道如何返回所有值。這可能是我的問題所在,但不幸的是,我對foreach循環並沒有那麼強大。 – 2014-10-02 01:02:03