Magento: Setting Category Meta Title, Keywords, and Description
As a developer working on Magento e-commerce websites, I frequently encounter issues where the default category meta title, description, and keywords are not properly applied to product listings. In this post, we’ll dive into the underlying cause of this issue and provide step-by-step solutions.
Understanding the Magento Category Meta Data
In Magento, each category has its own data set including meta_title, meta_keywords, and meta_description. These fields are available in the admin panel under Catalog > Categories.
<p class="admin__field-control">
<input type="text" name="meta_title" value="" data-ui-id="page-meta-title" id="meta_title" title="Meta title for the category page.">
</p>
<p class="admin__field-control">
<textarea rows="3" cols="50" name="meta_keywords" data-ui-id="page-meta-keywords" id="meta_keywords" title="Meta keywords for the category page, separated by commas." style="height:60px;"></textarea>
</p>
<p class="admin__field-control">
<textarea rows="3" cols="50" name="meta_description" data-ui-id="page-meta-description" id="meta_description" title="Meta description for the category page. This text appears in search engines under the page title." style="height:60px;"></textarea>
</p>
Why Meta Data Is Not Applied by Default
The default Magento configuration does not apply these meta fields to the product listing pages automatically. The category_view.xml layout file contains no reference to the category meta data.
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/empty.xsd">
<update handle="catalog_category_anchor"/>
<referenceContainer name="content">
<block class="Magento\Catalog\Block\Category\View" name="category.products" as="product_list" template="Magento_Catalog::category/products.phtml"/>
</referenceContainer>
</layout>
Setting Meta Data with Layout Files
To apply the category meta data to your product listing pages, you need to set it in a layout update file. Create or edit catalog_category_view.xml under your theme’s layout folder.
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="catalog_category_anchor"/>
<referenceContainer name="head.extend">
<block class="Magento\Theme\Block\Html\Title" name="category.title" template="Magento_Theme::html/title.phtml"/>
<block class="Magento\Theme\Block\Html\MetaKeywords" name="category.keywords" template="Magento_Theme::html/meta-keywords.phtml"/>
<block class="Magento\Theme\Block\Html\MetaDescription" name="category.description" template="Magento_Theme::html/meta-description.phtml"/>
</referenceContainer>
<update handle="catalog_category_anchor"/>
</page>
Notice that we are using the head.extend container to add our meta title, keywords, and description blocks. These blocks use the default Magento templates to generate the appropriate HTML tags.
Conclusion
In summary, Magento does not apply category meta data to product listings by default. To fix this issue, you need to manually set the meta data in a layout update file under your theme folder.
Remember to flush the cache after making these changes for them to take effect. You can do this via System > Cache Management or using the command line:
bin/magento cache:flush
If you’re looking for more advanced SEO techniques and best practices, I highly recommend checking out our comprehensive guide on Magento SEO Optimization.
Happy coding!