gantry5
gantry5 copied to clipboard
Is it possible to change meta tag based on URL query?
I am making a very simple product page for my site. It is like https://mystie.com/product?id=13367, where I use javascript get the product info from a JSON api:
$.get('https://jsonapi.url/?id=13367',function(product){
displayProductPage(product)
})
For this page I use gantry's cutsom page and put a cutsom module on it, the function displayProductPage
will render html for the product and append it to the custom module.
It's after I finish the html/css/javascript part when I realize I missed one important thing: I need to set the meta tags according to the product id: og:image, og:description etc., so when people share the page Facebook will get the correct info of the product.
Is there a way in Gantry's arsenal to set the meta tag according to the URL query?
Whatever works in Joomla, should also work in Gantry.
https://stackoverflow.com/questions/57468123/joomla-add-some-meta-tags-one-page
Thanks. I've read this but I have very limited knowledge of component development. So could you please tell me where I should put these codes? I've checked gantry template's index.php file but I don't think it is "the section where it build up the head content". And this seems to require editing core gantry files. Is it possible to do it in an Atom?
It really depends, but if you have a particle with the product data, you can use something like {% do joomla.factory.document.setMetaData('og:title', title) %}
. If you have the data in somewhere else, you need to do the same in PHP.
Thanks. I've tried creating a custom html particle, and modify the custom.html.twig file to somehting like
{% extends '@nucleus/partials/particle.html.twig' %} {% do joomla.factory.document.setMetaData('og:title', 'FB_Test_Title') %}
{% block particle %} {% do joomla.factory.document.setMetaData('og:title', 'FB_Test_Title') %} {% set html = particle.twig ? gantry.theme.compile(particle.html) : particle.html %} {{ (particle.filter ? gantry.platform.filter(html) : html)|html|raw }} {{ "Test String" }} {% endblock %}
But it didn't work - while the "Test String" does display on my page, the og:title meta tag is not set. What did I do wrong? I've checked all .twig files on all of my sites but I couldn't find any instance of usage "joomla.factory", so I really can't figure this out by myself.
Also, the other problem I need to solve is how to pass the product info to the particle. Now I am thinking to use the "custom function" to retrieve product data from the JSON API (A live example of the API is https://sheet2api.com/v1/CHN84f1jQ6iL/church-list-improved?ID=80541, and my live page in development is "https://cchc-herald.org/churchs?id=80541") and then hopefully I can use the data in the .twig file. Am I on the right track?
Hmm, not sure, maybe you should do it in an atom instead? Or maybe plugin works? I have never used the metadata method in Joomla, so I was just googling it.
Regarding to the other question, check how contentarray particle works. It has both the JS you need and it reads the variables from ajax.xxx
variables.