Custom Metadata in News articles

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
JamesT
Forum Members
Forum Members
Posts: 175
Joined: Tue Sep 08, 2015 10:41 am

Custom Metadata in News articles

Post 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?
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1609
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Custom Metadata in News articles

Post by DIGI3 »

In your page template add something like

Code: Select all

{if $metadata}{$metadata}{/if}
In your news template add

Code: Select all

{$metadata="your metadata tag here" scope=global}
Not getting the answer you need? CMSMS support options
User avatar
Rolf
Dev Team Member
Dev Team Member
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

Re: Custom Metadata in News articles

Post 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
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
JamesT
Forum Members
Forum Members
Posts: 175
Joined: Tue Sep 08, 2015 10:41 am

Re: Custom Metadata in News articles

Post 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?
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1609
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Custom Metadata in News articles

Post 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.
Not getting the answer you need? CMSMS support options
JamesT
Forum Members
Forum Members
Posts: 175
Joined: Tue Sep 08, 2015 10:41 am

Re: Custom Metadata in News articles

Post by JamesT »

How do I create a custom input field for the news module?

Edit: Ignore me, I found it! Thanks.
JamesT
Forum Members
Forum Members
Posts: 175
Joined: Tue Sep 08, 2015 10:41 am

Re: Custom Metadata in News articles

Post 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?
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1609
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

Re: Custom Metadata in News articles

Post 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.
Not getting the answer you need? CMSMS support options
JamesT
Forum Members
Forum Members
Posts: 175
Joined: Tue Sep 08, 2015 10:41 am

Re: Custom Metadata in News articles

Post 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.
JamesT
Forum Members
Forum Members
Posts: 175
Joined: Tue Sep 08, 2015 10:41 am

Re: Custom Metadata in News articles

Post 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}
Post Reply

Return to “CMSMS Core”