Yii2 分页面设置 keywords、description

4 篇文章 0 订阅
3 篇文章 0 订阅

原文链接: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');
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值