wp head - How to add meta tags inside single image page?

admin2025-06-04  5

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.

Share Improve this question asked Jan 14, 2019 at 23:57 MariuszMariusz 234 bronze badges 1
  • Ok i fix the problem, my code was inside loop (i know it was stupid ) it should be on top of page before get_header(); – Mariusz Commented Jan 15, 2019 at 0:11
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749011818a315601.html

最新回复(0)