kundi
December 17, 2016, 1:25pm
1
Hello,
I have a question: I have a custom model Article derived from Page, which defines getCoverImage() function:
public function getCoverImage() {
if ($this->coverImage() != '')
return $this->image($this->coverImage());
return false;
}
How can I use the $page property in the global template to call this function?
I want to show the image in the facebook open graph tags in the header of the page: in header.php snippet for a particular article:
<?php if ($page->getCoverImage()): ?>
<meta property="og:image" content="<?php echo $page->getCoverImage()->url() ?>" />
<?php endif ?>
Thanks
texnixe
December 17, 2016, 2:10pm
2
Don’t know if this is the best way to handle this, but should be safe:
<?php if(method_exists($page , 'getCoverImage' ) && $image = $page->getCoverImage()): ?>
<meta property="og:image" content="<?php echo $image->url() ?>" />
<?php endif ?>
kundi
December 17, 2016, 2:57pm
3
Would that really work if the method is defined on Article class derived from Page? Or should I define this function on the Page model instead somehow?
You can define a custom page method in a plugin to make it available for different templates as described here .
texnixe
December 17, 2016, 4:35pm
5
Yes, it will work. But as @flokosiol suggested, you can define a custom page method instead of a page model. You can stick with the page model if you don’t need that method anywhere else.