Forum
Trouble with social sharing in blog
Hi guys,
So I'm trying to set up the facebook and twitter card sharing. I've tried following the solution docs on it and it is working great for the pages but I can't see any of the tags in the header when I view source and I'm not sure exactly what I'm doing wrong.
<?php
# get the post
$result = perch_blog_custom(array(
'section' => 'comic-pages',
'filter' => 'postSlug',
'match' => 'eq',
'value' => perch_get('s'),
'template' => 'blog/post_comic_page.html',
'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/myfbname',
));
# display the post
echo $result['html'];
?>
Here is the header code...
<?php
$domain = 'https://'.$_SERVER["HTTP_HOST"];
$url = $domain.$_SERVER["REQUEST_URI"];
$sitename = "Heart of Millyera";
$twittername = "@HeartOfMillyera";
$sharing_image = '/images/social_tile_fallback_img.jpg';
PerchSystem::set_var('domain',$domain);
PerchSystem::set_var('url',$url);
PerchSystem::set_var('sharing_image',$sharing_image);
PerchSystem::set_var('twittername',$twittername);
perch_page_attributes(array(
'template' => 'default.html'
));
?>
I'm going to run this up here and see. There's an alarmingly high number of people having problems with this, so it must be something we're doing wrong.
No problems. Happy to share my git repo with you if that helps at all, just let me know. :)
Hi Drew, sorry to bug you but is there any progress with this issue?
Sorry, Jana, I'll see if I can look at this tomorrow.
Sorry to bug you again Drew but the site I'm using this on is due to go live in a week, is there any word on this?
Sorry, Jana - I'll get something together for you when I'm back in the office on Tuesday.
no problem, thanks!
How'd you go Drew?
Literally on it now! I'm implementing it on my own blog to try and find the issue.
Great! Thanks so much.
Hey Drew how did you go?
It's all working flawlessly for me. Can I just double check you're calling
perch_page_attributes_extend()
before you useperch_page_attributes()
?Oh I wasn't but I still can't get it to work. I have a global header and I have moved the extend and related to above the perch layout but It's still not working for me. :(
Do I need to pass variables into the global header template? Sorry my php is pretty beginner.
Here is my new extend.
Sorry for the delay in getting back to you. Australian/UK time difference makes it a bit hard :p
Header code is the same as above.
And then
global.header
is callingperch_page_attributes()
? Can you show me that part?You mean this yes?
This part works fine on my normal pages, but doesn't display at all on the blog posts
Ok, it looks like you're overwriting what you've already set.
On the blog page, change this:
to this:
That will set 'blog' as a layout var that we can then test for.
So, in
global.header
change this:to this:
This will only reset those variables if it's not a blog page.
I've done those changes but I still don't get any of the page attributes loading into the header. I added some comments and it looks like even though I'm setting blog to true, the header doesn't think I'm setting blog to true. :(
Those comments you've added are the wrong way around.
<!-- no blog for you -->
will show when you're on a blog page.Ok so I had a friend who is more experienced with PHP but not perch try and help me out. We kinda got it to work but it seems pretty hacky. Is there a working git example online somewhere?
We had to add an index.php page and a blog/post.php into the admin, add page titles, images etc to those pages and then the blog meta tags would show up(not the ones on the page equivalent), but the site title doesn't show up on either index.php or post.php and none of the images are working for post.php pages. :( Site title whatever, I can just hard code, but I do really need to get the images working and I'm not sure why they aren't when the descriptions are.
post.php
global.header.php
facebook.html
View source output...
We've just released Blog 5.0 to make this simpler. It has a dedicated section for entering Facebook and Twitter meta data, and a simple function to output it all in the head of your page.
Blog: https://grabaperch.com/add-ons/apps/blog
New page function: https://docs.grabaperch.com/addons/blog/page-functions/perch-blog-post-meta/