對于下面的觀察者類,我正在嘗試在類別頁面上設定 noindex 和 nofollow。我無法讓代碼首先檢查它是否是類別頁面,然后如果類別頁面的標題在類別名稱中包含“部分”,則設定 noindex。你能幫我找出來嗎?
<?php
namespace Perfectmakeupmirrors\PmmHead\Observer;
use Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;
use Magento\Framework\Registry;
use \Psr\Log\LoggerInterface;
class SetRobotsMetaTag implements ObserverInterface
{
protected $request;
protected $registry;
protected $layoutFactory;
protected $logger;
public function __construct(
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\Registry $registry,
\Magento\Framework\View\Page\Config $layoutFactory,
LoggerInterface $logger)
{
$this->registry = $registry;
$this->request = $request;
$this->layoutFactory = $layoutFactory;
$this->logger = $logger;
}
public function execute(Observer $observer) {
$this->logger->debug("Observer is watching");
$this->logger->debug("Action Name = " . $this->request->getActionName());
$this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');
if ($this->request->getActionName() == 'category') {
$category = $this->registry->registry('current_category');
$categoryName = $category->getName();
$this->logger->debug("Category Name = $categoryName");
if (stripos($categoryName, 'part') !== false) {
$this->logger->debug("Robots Set");
$this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');
}
}
}
}
uj5u.com熱心網友回復:
更新您的功能條件。
public function execute(Observer $observer) {
$category = $this->registry->registry('current_category');
$categoryName = $category->getName();
$categoryName = trim($categoryName);
$this->layoutFactory->setRobots('INDEX,FOLLOW');
if (stripos($categoryName, 'Parts') !== false) {
$this->layoutFactory->setRobots('NOINDEX,NOFOLLOW');
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/476725.html
上一篇:未設定會話時發生重定向回圈
