-2

我想从中删除元关键字标签中的尾随逗号:

<meta name="keywords" content="enhancing,nutritional,supplements," />

对此:

<meta name="keywords" content="enhancing,nutritional,supplements" />

这是完整的代码。

/* 自动为 Genesis 框架中的 SEO 生成帖子/页面元关键字。如果 Genesis 没有返回关键字,则关键字是从帖子标签或
帖子/页面标题自动生成的。*/

add_action( 'genesis_meta', 'my_auto_keywords' , 11 );
function my_auto_keywords(){
if(
!is_singular() ||
genesis_get_custom_field( '_genesis_keywords' ) ||
genesis_get_custom_field( '_aioseop_keywords' ) ||
genesis_get_custom_field( 'thesis_keywords' ) ||
genesis_get_custom_field( 'keywords' )
) return;

$tags = get_the_tags(); #wp

if ($tags) {
    foreach ($tags as $tag) {
        $keywords .= $tag->name . ', ';
    }
}

if ($keywords) {
    $keywords = '<meta name="keywords" content="' . $keywords . '" />';  
}
else {
    $title = get_the_title();
    $keywords = preg_split("/[\s,]+/", $title);
    $keywords = array_map('strtolower', $keywords);
    $keywords = array_diff($keywords, my_excluded_words()); //remove useless words
    foreach ($keywords as $keyword)
        $kw_list .= $keyword . ',';
    $keywords = '<meta name="keywords" content="' . $kw_list . '" />';   
}

echo $keywords . "\r\n";
}

function my_excluded_words(){
return array ("testword", "testword2", "testword3", "testword4");
}
4

2 回答 2

-1

替换这个:

if ($tags) {
    foreach ($tags as $tag) {
        $keywords .= $tag->name . ', ';
    }
}

有了这个:

if ($tags) {
    foreach ($tags as $tag) {
        $keywords[] = $tag->name;
    }
    $keywords = implode(',', $keywords);
}

并替换它:

foreach ($keywords as $keyword)
    $kw_list .= $keyword . ',';

有了这个:

$kw_list = implode(',', $keywords);
于 2014-11-11T00:10:30.610 回答
-1

也许尝试:

$keywords = join(",", array_map(function($tag){ return $tag->name; }, $tags ));
if($keywords) {
  $keywords = '<meta name="keywords" content="' . $keywords . '" />';
}
于 2014-11-11T00:31:34.150 回答