pedroborges / kirby-meta-tags

⬢ HTML meta tags generator for Kirby. Supports Open Graph and Twitter Cards out of the box.
MIT License
97 stars 11 forks source link

Access to method Children #6

Closed isaactopo closed 6 years ago

isaactopo commented 6 years ago

Hi Pedro, there's a way to get this working?: $page->children()->find('contact-info')->email()

I can't access to this field using children()->find(). Thanks for the great plugin!

pedroborges commented 6 years ago

This would only work if every page on your website had a contact-info child page. Have you tried page('contact-info')->email()?

isaactopo commented 6 years ago

Thanks for your reply, the code is something like this:

c::set('meta-tags.templates', function(Page $page, Site $site) {
    return [
        'gym' => [
        'json-ld' => [
             'ExerciseGym' => [
            'name' => 'whatever '. $page->title()->value(),
            'image' => $page->image($page->briefImage())
                    ? $page->image($page->briefImage())->url()
                             : site()->images()->sortBy('sort', 'asc')->first()->url(),
             'url' => $site->url(),
             'telephone' => $page->children()->find('contact-info')->tel(),
          ]
        ]
        ]
    ];
});

As you say I tried to access with method page('some-page') but does not work as well. It seems imposible to work with children and page methods. $page->title()->value() is working fine.

pedroborges commented 6 years ago

Please try: page('contact-info')->tel()->value()

isaactopo commented 6 years ago

ok, the value() does the trick. Thanks for your help.

pedroborges commented 6 years ago

Under the hood the plugin used json_encode to generate the JSON-LD markup. Kirby fields are Field objects and need to be converted to string before, that's what the value() method does, it converts the object to string.