要做到对搜索引擎友好,一般都应该在页面中设置Keywords, Description这样的Meta数据,我们的Drupal网站中很多都是用PHP来实现的,代码如下:
$meta="
";
$element = array(
'#type' => 'markup',
'#markup' => $meta,
);
drupal_add_html_head($element, 'meta');
但是发现在keywords, description里面包含有单引号时候会让html错误,特别是近期添加amp版本的时候,google webmaster tools或者amp validator会报错。
这时候可以这样修改:
$meta="
";
$element = array(
'#type' => 'markup',
'#markup' => $meta,
);
drupal_add_html_head($element, 'meta');
但keywords、description还有时会遇到来源数据中包含双引号的问题,我们只好在此前进行替换,把双引号替换成单引号,代码如下:
$keywords_meta = str_replace('"',"'",$meta_keyword);
$description_meta = str_replace('"',"'",$description);
$meta="
";
$element = array(
'#type' => 'markup',
'#markup' => $meta,
);
drupal_add_html_head($element, 'meta');
这样才算是能彻底解决这种因为引号报错的问题。在Drupal网站中,这种修改可能涉及到模板、模块、页面、Views、Block等各种使用到PHP代码来设置meta数据的地方,检查起来好麻烦。