我们如何像在Magento 1中一样在Magento 2中创建站点地图页面?
http://example.com/catalog/seo_sitemap/
我们如何像在Magento 1中一样在Magento 2中创建站点地图页面?
http://example.com/catalog/seo_sitemap/
Answers:
Magento 2:默认情况下不提供HTML Sitemap。
可以使用免费扩展:
https://www.mageplaza.com/magento-2-seo-extension/
您有以下选择
CMS页面:可以参考Magento 1文章/programming/5020257/creating-a-magento-sitemap-page然后将其转换为Magento 2将对CMS页面有所帮助
对于类别和子类别:在magento 2中获取所有可用的类别,子类别列表
对于类别产品:Magento 2:使用类别ID获取产品集合
从Magento 2.1.8版开始,默认情况下,站点地图功能包含在管理员中。
定义站点地图位置:营销> SEO和搜索>站点地图。您也可以从此处手动生成站点地图文件。
配置cron设置:商店>配置>目录> XML站点地图
以下是设置定期cron作业的说明。就像Magento 1.x一样,系统的cron调用Magento cron脚本来计划作业。
Magento 2没有像magento 1.xx这样的类别和产品的html网站地图url。为此,您可以创建CMS页面(例如“ sitemap /”)并调用phtml文件
其中将包含用于调用所有类别的自定义代码
在cms页面中,调用此命令:
{{block class="Magento\Framework\View\Element\Template" template="Magento_Theme::sitemap.phtml" name="customsitemap"}}
然后在phtml中,调用此命令:
<?php
$objectManagerr = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManagerr->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categoryy = $categoryFactory->create()
->addAttributeToSelect('*');
//->addAttributeToFilter('is_active',1)
//->addAttributeToFilter('is_anchor',1);
$excludedCategory = array(0);
?>
<ul class="sitelist">
<?php foreach ($categoryy as $cc): ?>
<?php if(!in_array($cc->getId(), $excludedCategory)):?>
<div class = "sitemap-list" style="float:left;">
<?php if ($cc->getLevel()==2):?>
<li>
<h3><a href="<?php echo $cc->getUrl(); ?>"><?php echo $cc->getName(); ?></a></h3>
<?php
if($cc->getChildren())
{
$sub = explode(",", $cc->getChildren());
$categoryFactorysub = $objectManagerr->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categoryysub = $categoryFactorysub->create()
->addAttributeToSelect('*');
$subcat=$categoryysub->addFieldToFilter('entity_id', array('in' =>$sub));
//$subcat->printlogquery(true);exit;
?>
<ul style="margin-left:15px;">
<?php foreach ($subcat as $subcategories){ ?>
<?php if ($subcategories->getLevel()==3):?>
<div class = "sitemap-list">
<li>
<?php if($subcategories->getImageUrl()): ?>
<div class="mega-menu-ad">
<img src="<?php echo ($subcategories->getImageUrl()); ?>" width="25" height="25" />
</div>
<?php endif; ?>
<a href="<?php echo $subcategories->getUrl(); ?>"><?php echo $subcategories->getName(); ?></a>
<?php
if($subcategories->getChildren())
{
$subSubCat = explode(",", $subcategories->getChildren());
$subsubCatFactory = $objectManagerr->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$subSubCatCollection = $subsubCatFactory->create()
->addAttributeToSelect('*');
$subSubCollection=$subSubCatCollection->addFieldToFilter('entity_id', array('in' =>$subSubCat));
?>
<ul style="margin-left:20px;">
<?php foreach ($subSubCollection as $subSubcategories){ ?>
<div class = "sitemap-list">
<li>
<?php if($subSubcategories->getImageUrl()): ?>
<div class="mega-menu-ad">
<img src="<?php echo ($subSubcategories->getImageUrl()); ?>" width="25" height="25" />
</div>
<?php endif; ?>
<a href="<?php echo $subSubcategories->getUrl(); ?>"><?php echo $subSubcategories->getName()?></a>
</li>
</div>
<?php } ?>
</ul>
<?php } ?>
</li>
</div>
<?php endif;?>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php endif;?>
</div>
<?php endif;?>
<?php endforeach; ?>
</ul>
这样,您可以轻松地在Magento 2中创建站点地图URL。
您可以使用Sitemap扩展名(https://marketplace.magento.com/fme-advanced-sitemap-generator.html),这将帮助您无需编程即可完成工作,或者您可以看一下本文:https ://www.fmeextensions.com/blog/generate-google-optimized-sitemap-xml-in-magento-2/