Page 1 of 1
Custom Metadata in News articles
Posted: Sun Aug 01, 2021 11:39 am
by JamesT
I would like to add some Page Specific Metadata into News articles but there is no "Logic" tab on the News module like there is for Pages.
Can this be done?
Re: Custom Metadata in News articles
Posted: Sun Aug 01, 2021 3:15 pm
by DIGI3
In your page template add something like
In your news template add
Code: Select all
{$metadata="your metadata tag here" scope=global}
Re: Custom Metadata in News articles
Posted: Sun Aug 01, 2021 3:31 pm
by Rolf
You can create custom field(s) and use them to fill in the metadata. This tutorial will be a good start:
https://cmscanbesimple.org/blog/base-cm ... d-metatags
Re: Custom Metadata in News articles
Posted: Sun Aug 01, 2021 4:16 pm
by JamesT
Thanks. Specifically I want to set an an og:image image for each News article. I see you cover this with these two lines:
Code: Select all
{$page_image = $page_image|default:"{$theme_url}/page_image.png" scope=global}
Code: Select all
<meta property="og:image" content="{$page_image}">
The issue is that you have specified a static "page_image.png" whereas I need a different image for each News article. Where would I enter this data within the News article admin area?
Re: Custom Metadata in News articles
Posted: Sun Aug 01, 2021 5:12 pm
by DIGI3
Make an image field for news articles, then test for it and assign it to $page_image in your news template, being sure to set the scope to global.
Re: Custom Metadata in News articles
Posted: Sun Aug 01, 2021 5:32 pm
by JamesT
How do I create a custom input field for the news module?
Edit: Ignore me, I found it! Thanks.
Re: Custom Metadata in News articles
Posted: Sun Aug 01, 2021 6:01 pm
by JamesT
One final thing, when metadata is set in the news article, I do not want the page specific metadata output, otherwise I will get two og:image lines output.
Can I supress the page specific metadata in this case, whilst still outputting the global metadata?
Re: Custom Metadata in News articles
Posted: Sun Aug 01, 2021 6:11 pm
by DIGI3
yep, anything in a template can be wrapped in {if} statements. {if !$my_custom_metadata]{metadata}{/if}
This can end up being a bit tricky as {metadata} includes global metadata as well as page-specific, so you want to plan ahead how you do this. Personally I create fields for all metadata in my page template, so the page-specific metadata field should only be used on the very rare occasion you need to add something unique to the page. I wouldn't actually put og tags or titles in there. That way you can test for each field in your page template, or set defaults like: <meta property="og:image" content="{$customogimage|default:$ogimage|default:$defaultogimage}">
In the above example $ogimage would be a field in your page template, $customogimage could be set in modules like News, and then $defaultogimage defined as something to use if neither of those are set.
There's lots of different ways to do it all, this is just one example to get the gears turning.
Re: Custom Metadata in News articles
Posted: Sun Aug 01, 2021 6:16 pm
by JamesT
I may have to supress {metadata} altogether then and duplicate the global metadata in the news module, since a significant number of og:image entries have already been made in the page-specific metadata sections.
In any case, very helpful, thanks.
Re: Custom Metadata in News articles
Posted: Mon Aug 02, 2021 2:06 pm
by JamesT
I came up with this to replace page og:image with news og:image. I'm sure it could be much better coded but it works!
Code: Select all
{if !empty($entry->fields.Page_Specific_Metadata->value)}
{capture name=metadata assign=metadata}
{metadata}
{/capture}
{capture name=metadata assign=metadata}
{$metadata|regex_replace:"/<meta property=\"og:image\" content=\".*?\" \/>/":""}
{/capture}
{$new_meta = $entry->fields.Page_Specific_Metadata->value}
{$metadata = "`$metadata`$new_meta" scope=global}
{/if}