简体   繁体   中英

How do I dynamically generate og meta data for Facebook sharing based on content on that page?

I'm trying to figure out a simple way of using PHP to dynamically generate OG meta information for Facebook sharing such as og:title, og:description and other tags based on content that is already on a page.

Here's the URL for reference: http://jonathanmitchell.ca/OPEC/new3.php

The og:title I want to draw from is the "This is the title" which has an ID of news-story-title-on-template.

Any help at all would be really appreciated ... thanks!

You can generate the dynamic og tags.

For eg you want to assign og tags the values according to an ID-

Your object url: http://myobject.com?id=ID

<?php
$id = $_GET['id'];
// manipulate title and image with this id, say $title / $image
?>
<meta property="og:url" content="http://myobject.com?id=<?php echo $id; ?>" />
<meta property="og:app_id" content="APP_ID" />
<meta property="og:title" content="<?php echo $title; ?>" />
<meta property="og:image" content="<?php echo $image; ?>"/>

If you are passing any parameter to your object url, make sure you add the same to the og-tag: og:url (as I've shown in above example)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM