从帖子标题自动生成关键字

时间:2012-07-03 23:04:51

标签: php wordpress split title

例如我有“战舰预告片”的标题 所以它会生成这样的关键字 “最好,玩家,世界” 我怎么能这样做?

我想像这样的html apear ...

First:The
Second:Battleship
Third:Trailer

<meta name="keywords" content="<FirstWord>,<second>,<third>"/> 

更确切地说,我想用文字分割标题。

我使用Wordpress

3 个答案:

答案 0 :(得分:5)

echo '<meta name="keywords" content="'.implode(',', explode(' ', $string)).'"/>';

这会将$string中空格分隔的单词与逗号连接起来。显然将$string替换为适当的变量。

答案 1 :(得分:1)

$string = get_the_title(); // Inside your header.php 
echo '<meta name="keywords" content="'.implode(',', explode(' ', $string)).'"/>';

参考:http://codex.wordpress.org/Template_Tags/get_the_title

参考:http://php.net/manual/en/function.implode.php

答案 2 :(得分:1)

获得帖子的slu might可能会更好。这将消毒并删除任何对搜索引擎没有任何价值的短语。然后用PHP爆炸和内爆...

如何:

 // Get the global post variable (inside or outside the loop)
 global $post;

 // Seperate title slug by dashes into array
 $the_slug_into_array = explode('-', $post->post_name);

 // Convert array back into a string with comma separation
 $the_keywords = implode(',', $the_slug_into_array );

 // Echo out the string of terms from the title
 echo $the_keywords;

请告诉我这是否对您有用......