简体   繁体   中英

How add meta tags to <head> after load page?

How can I add meta tags to <head> using PHP?

I have this structure in index.php:

<Html>
<Head> ... </ head>
<body> <? php include ($ module); ?> </ Body>
</ Html>

Depending on the subpage, I would like to display a different page title. I did the functions meta_tags ($ page) where $ page is the current subpage, but now I have a subpage with an announcement generated from the database. I came up with something like this:

<? php ....
$ js. = "<script> $ ('meta [name = \" description \ "]'). attr ('content', '". $ row [' product_title ']. "'); </ script>" ;
?>

And it works nicely, but after gluing on FB, it does not read this meta tag.

From what I learned, FB does not see JS and hence the problem.

It all comes down to the fact that he does not want to query the database twice for the title of the product. Once on the subpage of the product, and the second time in the above-mentioned function.

What to do?

Yes, some search engines don't allow the js load while they are crawling, so a good way to do it by PHP.

<?php
        //Enter your code here, enjoy!

function meta_into_displayer($product_id){

    /*fetch the product data from database here*/


    /*lets you got following array data*/
    $product=[

        "title"=>"Product Title",
        "description"=>"This is product description",

        ];

    return $product;
}

$product_data=meta_into_displayer(1);
echo '
<html>
<head>
   <title>'.$product_data['title'].'</title>
   <meta name="description" content="'.$product_data['description'].'">
</head>
<body>
</body>
</html>'
?>

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