2014-10-02 66 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