Use magento2.1 Need to generate meta title and description for my products dynamically, forexample: BASEURL-for your(Static string)-Product title, and for meta description example: static string-attribute1-attribute2


1 Answer 1


I just override

File: module-catalog/Block/Category/View.php

And add the below code. I am just providing you a logic. I have directly used a custom query. I have created a table to add URL, title, description, and keyword then I have matched current URL in the table and then set title, description, and keyword dramatically.

$urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');
        $current_url = $urlInterface->getCurrentUrl();

        if ($current_url)
            $sqlQueryCustom_url_meta_data= "SELECT * FROM custom_url_meta_data where url='".$current_url."'";
                $title = $resultCustom_url_meta_data[0]['meta_title'];
                $description = $resultCustom_url_meta_data[0]['meta_description'];
                $keywords = $resultCustom_url_meta_data[0]['meta_keywords'];

Hope it works!

  • Thanks, could you please let me know how can i set sample static strings into this code?
    – MGPM
    Commented Mar 18, 2020 at 9:26
  • you can directly set string to $this->pageConfig->getTitle()->set('title test'); $this->pageConfig->setDescription('description test'); $this->pageConfig->setKeywords('keyword test');
    – Devidas
    Commented Mar 18, 2020 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.