Closed philipschilling closed 5 months ago
So the way you will want to do this will be using the SEOmatic APIs... something like this:
{% set rentalPrice = entry.rentalPrice %}
{% do seomatic.meta.seoDescription(entry.metaDescription|replace({'[rentalPrice]': rentalPrice})) %}
See here for details: https://nystudio107.com/docs/seomatic/using/#seomatic-variables
Also you generally don't need a separate metaDescription
field in Craft, typically you can set up content mapping in SEOmatic -> Content SEO as discussed here: https://nystudio107.com/docs/seomatic/overview.html#the-meta-cascade
Or if you really do need a per-entry custom SEO overrides, you can use an SEO Settings field so things will work more automatically for you: https://nystudio107.com/docs/seomatic/fields.html
Thank you, Andrew. It works and was exactly what I was looking for. I read earlier the pages of the last 2 links. I decided to create and use separate meta fields for 2 reasons assuming that SEOmatic stores its data separate from or different to Craft CMS:
Honestly, I don't know how SEOmatic technically works and where it stores the data. I just didn't want to loose it or re-enter it when the plugin changes (bug, discontinuation, price increase, etc.).
Okay, sounds good. Generally you don't need a separate meta description, as pulling from existing content via Content SEO usually works better than manually written descriptions/titles, because they reflect the content on-page.
Question
I would like to include an entry variable in the meta description. Specifically, I have stored in a field of an entry a price which I would like to output in the meta description which is a field of the same entry type. The issue is that the meta tags are included dynamically into the pages and I cannot edit them in templates.
I found this solution which looked promising: https://craftcms.stackexchange.com/questions/39543/can-i-define-and-use-global-variables-within-content-fields
Global Variables
I added the global variable
{rentalTerms.rentalMinimumPeriod} {rentalTerms:rentalMinimumPeriod}
just to see what happens but the output is(rentalTerms.rentalMinimumPeriod) (rentalTerms:rentalMinimumPeriod)
. So, I didn't bother to try with entry variables.Replace Function
I added the following function directly after the
<head>
tag in my template:I added the placeholder [rentalPrice] to my Meta Description field:
It is replacing the variable but outputs the text directly after the
<body>
tag:In the meta description tag the variable has not been replaced:
Do you have a solution?