原文链接:https://blog.csdn.net/wang78699425/article/details/80570994
Yii2 分页面设置 keywords、description
需求:如果页面有自己的 keywords,description,使用页面的TDK;如果页面没有自己的 keywords,description,使用默认的 keywords,description。
- layout文件中:main.php的写法:
<head>
<meta charset="<?= Yii::$app->charset ?>">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="applicable-device" content="mobile">
<meta name="format-detection" content="telephone=no" />
<meta name="format-detection" content="email=no" />
<meta name="format-detection" content="address=no;">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="default" />
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode($this->title) ?></title>
<?php
//注意key值(即:$this->metaTags中的 keywords、description)与页面上的key值对应
!isset($this->metaTags['keywords']) && $this->registerMetaTag(["name" => "keywords", "content" => params('header_keyword')]);
!isset($this->metaTags['description']) && $this->registerMetaTag(["name" => "description", "content" => params('header_description')]);
?>
<?php $this->head() ?>
</head>
- 页面文件的写法:
<?php
use common\components\GenerateUrl;
use common\helpers\RedisHelper;
use wechat\models\Goods;
$this->title = $goods['seo_title'];
$this->registerMetaTag(["name" => "keywords", "content" => $goods['seo_keywords']], 'keywords');
$this->registerMetaTag(["name" => "description", "content" => $goods['seo_describe']], 'description');