how to access post date?
Closed this issue · 1 comments
MattAppleton commented
We are trying to add a publish date to our metadata output (Lined in seems to want this!)
So far we tried just 'postDate' or a function like this added to seomate.php (see below) — with no joy!
Any advice on picking up the post date would be much appreciated!
'additionalMeta' => [
'og:type' => 'website',
'datePublished' => function ($context) {
$unformatted = $context->postDate;
$formattedDate = array(date($unformatted, 'd m Y'));
return $context;
},
'author' => [
'name' => 'name here',
'url' => 'url here',
],
],
aelvan commented
Context is the whole twig context for the request. So if it's a given requests entry
you're looking to get the publish date from, you'd do:
'datePublished' => function ($context) {
return isset($context['entry']) ? $context['entry']->postDate->format('d m Y') : '';
}
Please note that the closure should return a string with the value of the meta property. If the value is an empty string, it will be filtered out.