Implementing 'Facebook Open Graph Tags and Twitter Cards' on Blog article pages
Hi,
I’m trying to implement Facebook Open Graph Tags and Twitter Cards, following this article. I got it all working for the homepage, but I have trouble getting it to work on the blog article pages. The meta code doesn’t appear on the blog article pages.
I’m trying to use ‘perch_page_attributes_extend’ as described in the article, although my code is placed into a layouts template, named ‘global.header-blog.php’ which contains the entire top part code of the HTML page. This layout template file is then linked via php into ‘post.php’ file (which contains the blog template for my blog posts). This ‘post.php’ is located in a folder on the root of the site named ‘blog’.
I’m guessing it’s a problem with the path to the template that holds the meta tags, but not sure… as I don’t know PHP. Maybe the code is totally wrong here.
This is the code I have in place in 'global.header-blog.php’ (right below the link to my stylesheets and above the javascripts):
<?php
# get the post
$post = perch_blog_custom(array(
‘filter’ => ‘postSlug’,
‘match’ => ‘eq’,
‘value’ => perch_get(’s’),
‘skip-template’ => ‘true’,
‘return-html’ => ‘true’,
));
# set up the variables
$title = $post[‘0’][‘postTitle’];
$description = strip_tags($post[‘0’][‘excerpt’]);
if (isset($post[0][‘fbimage’])) {
$fbimage = $post[0][‘fbimage’];
} else {
$fbimage = ‘’;
}
# use the variables in the array value
perch_page_attributes_extend(array(
‘description’ => $description,
‘og_description’ => $description,
‘og_title’ => $title,
‘og_type’ => ‘article’,
‘sharing_image’ => $fbimage,
‘og_author’ => ‘https://www.facebook.com/frederik.backelandt',
‘template’ => ‘default.html’
));
?>
It might be something basic I’m missing here that isn’t explained in the article. I have no idea. Any help would be very much appreciated.
Thank you very much in advance, Veerle
Just to check - is the above all happening before the point in the page where the meta tags are output?
It's below this part:
and then below the Perch PHP code follows this :
Also, not sure if you need this info, but the post.php file code starts with this:
Ok, great. Where do you call
perch_page_attributes()
to output the result?Not 100% sure I understand the question, but I guess you mean the template that outputs/has these metatags?
At the end of the PHP code there is ‘template’ => ‘default.html’
That is the template that is located here: perch > templates > pages > attributes > default.html
Maybe that is wrong? Should I have created a different template in the folder 'blog' (located in perch > templates > blog)?
It's using
perch_page_attributes()
that will output the tags to the page.perch_page_attributes_extend()
is passing in the extra data, butperch_page_attributes()
creates the tags.If you don't have
perch_page_attributes()
anywhere, add it after theperch_page_attributes_extend()
.I have now this code in place in the 'global.header-blog.php' layout template:
It's placed in the 'head' element, where the metatags are located (as described before). This template is loaded into 'post.php' template page. It's still not giving any results. Nothings shows up :(
Well, that's very strange! If you add debug to your page, what does it output?
https://docs.grabaperch.com/docs/installing-perch/configuration/debug/
This is what it outputs on the blog page (blog.php):
I've got the same problem, though with a simpler php script.
Inside
<head>
onpost.php
isn't outputting any HTML. But it works onindex.php
.Thanks in advance, Eystein
Hi Eystein,
For blog (or other) app pages you need to use 'perch_page_attributes_extend' instead. It's explained in this article (scroll down a bit). It's what I try to do, but somehow I doesn't seem to work. Hope this helps.
Cheers, Veerle
Thanks Veerle,
I've tried it, and many workarounds, all day. Unfortunately without any luck either. I think it's time to sleep on it and tomorrow will be better.
fingers: crossed;
Cheers, Eystein
Can you try this:
Hi Drew,
Thank you for the help. I pasted the code in the 'global.header-blog.php' layout template(which is linked into post.php), replacing the old/previous PHP code with this code, but unfortunately the meta tags still don't appear :(
Cheers, Veerle
Is there any change at all?
Do you still get the same error? What code is on the line of the error?
I'm assuming you mean the part of the code that is in red? This:
Yes, what's on line 53 of
blog/post.php
?this:
FYI here is full PHP code (it's code I found to create previous and next buttons):
I guess
echo $result['html'];
shows the post andecho $post['html'];
isn't really needed?Yes, I think you can remove
it's not doing anything.
Hi Drew,
Just so you know: I removed this… unfortunately it didn't change anything else :( So the meta tags still don't show up on the blog pages.
Let me know if I can provide anything else to help you solve this weird issue.
Meanwhile, thanks again for all your efforts.
Cheers, Veerle