Hello im trying to add custom meta tags from facebook share button inside image.php file , and i dont know why they dont display, this is how my code looks:
function add_facebook_image_code(){
?>
<meta property="og:url" content="/<?=$post->post_name ?>/" />
<meta property="og:type" content="website" />
<meta property="og:title" content="example - <?=$post->post_title ?>" />
<meta property="og:image" content="<?=$post->guid ?>" />
<?php
}
add_action('wp_head', 'add_facebook_image_code');
I add this code inside image.php, is it possible to do it only inside that file ?
Thanks and best regards.
Hello im trying to add custom meta tags from facebook share button inside image.php file , and i dont know why they dont display, this is how my code looks:
function add_facebook_image_code(){
?>
<meta property="og:url" content="http://www.example/<?=$post->post_name ?>/" />
<meta property="og:type" content="website" />
<meta property="og:title" content="example - <?=$post->post_title ?>" />
<meta property="og:image" content="<?=$post->guid ?>" />
<?php
}
add_action('wp_head', 'add_facebook_image_code');
I add this code inside image.php, is it possible to do it only inside that file ?
Thanks and best regards.
Probably because when the execution is hitting your code is already too late to hook on wp_head
.
If you want to have your code in image.php, make sure you place it before get_header()
.
In any case, I would move the code to functions.php or dedicated file and maybe use is_attachment()
to conditionally print the meta tags.