Skip Navigation

[Resolved] opengraph

This support ticket is created 5 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 3 voices.

Last updated by Luo Yang 5 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#1369409

I have this function in my functions.php file to create opengraph code dynamically for each post:

/ Set your Open Graph Meta Tags
function fbogmeta_header() {
if (is_single()) {
//getting the right post content
$postsubtitrare = get_post_meta($post->ID, 'id-subtitrare', true);
$post_subtitrare = get_post($postsubtitrare);
$content = limit(strip_tags($post_subtitrare-> post_content),297);
?>
<meta property="og:title" content="Sermon Note: <?php the_title(); ?>"/>
<meta property="og:description" content="<?php echo $content; ?>" />
<?php
}
}
add_action('wp_head', 'fbogmeta_header');

But how do I get <meta property="og:description" content="<?php echo $content; ?>" /> to show my custom field "wpcf-notes-description" instead of "$content"? I can't seem to come up with the right code. Basically I want it to show the notes description of the current post in the og:description.

#1369609

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To get your custom field "wpcf-notes-description" value you can use the same WordPess function get_post_meta():

For example:

$notes_desc = get_post_meta($post->ID, 'wpcf-notes-description', true);

And try to use it as given under:
<meta property="og:description" content="<?php echo $notes_desc ; ?>" />

#1370111

So if I put this in the single page template, it shows the data inside the actual page: <?php $notes_desc = get_post_meta($post->ID, 'wpcf-notes-description', true);?> So technically the code works.

But if I put it in this function code into the functions.php file instead, so that I can use the data in the og tags, the content og tag ends up being empty still. Am I missing something?

function fbogmeta_header() {

   {
        //getting the right post content
        $postsubtitrare = get_post_meta($post->ID, 'id-subtitrare', true);
        $post_subtitrare = get_post($postsubtitrare);
        $content = limit(strip_tags($post_subtitrare-> post_content),297);
	$notes_desc = get_post_meta($post->ID, 'wpcf-notes-description', true);
        ?>  
       <meta property="og:description" content="<?php echo $notes_desc; ?>" />
        <?php
        }
        }
add_action('wp_head', 'fbogmeta_header');
#1371239

Hello,

Please try these, add a line in your PHP codes, for example:

function fbogmeta_header() {
 
   {
   global $post; // add this line
...

More help:
https://codex.wordpress.org/Global_Variables