简体   繁体   中英

How do sites put dynamic php content into facebook meta tags?

I've just added facebook meta tags to my news.php page. This file also dynamically generates article (urls) pages under it. But how am i to set the title for the fb meta tag in the head (ie. <meta property="og:title" content="<?php echo $data[0]['title']; ?>" /> ) when the $data is getting defined / populated lower down in the body?

How do so many sites do it?
Would be grateful for any guidance on this.

Below is the code for my page:

 <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Title</title>

    <meta property="fb:app_id" content="6666666666666"/>
    <meta property="og:title" content="<?php echo $data[0]['title']; ?>" />

         </head>

  <body>
  <script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '66666666666666',
      xfbml      : true,
      version    : 'v2.5'
    });
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script> 

    <header>


<div class="contentwrap">
            <article>
                <h1>- News- </h1>
                <?php 
                if (isset($_REQUEST['id'])) {// user gets here by clicking on link with id
                    $id_raw = trim(htmlentities($_REQUEST["id"]));
                    $data = news_data_id($conn, $id_raw);
                    ?>          

                    <h2><?php echo $data[0]['title']; ?></h2>
                    <img src="<?php echo $data[0]['filepath'];?>" style="float: left; max-height: 5em; margin: 0.5em 0.5em 0.5em 0; -webkit-shape-outside: circle(50%); shape-outside: circle(50%);">
                    <p><?php echo $data[0]['body']; ?></p>

    <div
  class="fb-like"
  data-share="true"
  data-width="450"
  data-show-faces="false">
</div>              


                <?php }
                else {  
(continues)

You'll have to use Output buffering control in your whole site to handle this.

Basically ob_start() on the first life of your file and ob_get_clean() to retrieve everything outputted during the execution.

I guess the best option to switch your website to mvc structure here is detailed article whot is mvc pattern : http://www.sitepoint.com/the-mvc-pattern-and-php-1/

For now without major changes you can change yoru news.php structure so that it will retrieve all content php data at the top before html part, so in that case you will have your $news variable at all places of your html and can easily create mattes and show content in body

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