dongwo5110 2014-08-04 17:18
浏览 104
已采纳

如何动态设置facebook元标记?

I have an opencart site, and I'm trying to configure facebook share options of my products.

Since everything is loaded as a separate module, I can't set the facebook meta tags like this (header.tpl):

<meta property="og:description" content="<?php echo $description; ?>" />

Because the $description doesn't exist while the header module is loading.It is created in the controller of product module. So I tried to change the content value dynamically (product.tpl):

$("meta[property='og:description']").attr('content','<?php echo $description; ?>');

And it worked, I can see that the value is changed (in the page source), then I debugged my page but I couldn't get the value.. I think I know the reason, I have to set the value before the page load but I'm not sure how can I do that.. do you have any idea ?

  • 写回答

1条回答 默认 最新

  • doulongdan2264 2014-08-06 07:22
    关注

    You could use the Document class to add those facebook tags (as many as you want). Just add two extra methods setFacebookDescription and getFacebookDescription, so you have to add the followings:

    <?php
    class Document {
        private $facebook_description;
    
        public function getFacebookDescription() {
            return $this->facebook_description;
        }
    
        public function setFacebookDescription($facebook_description) {
            $this->facebook_description = $facebook_description;
        }
    }
    

    On each controller, you will find at the end of each method, a call which loads the header of Opencart, something like this $data['header'] = $this->load->controller('common/header'); (example). Note that it might differ from yours, it depends on Opencart version.

    Now, in header.php controller you add:

    <?php
    class ControllerCommonHeader extends Controller {
    
        public function index() {
            $data['facebook_description'] = $this->document->getFacebookDescription();
        }
    }
    

    this will get the facebook_description variable and pass it to the view header.tpl. Next, add the facebook tags between your <head> tags in header.tpl file:

    <!DOCTYPE html>
    <head>
        <?php if ($facebook_description != '') { ?><meta property="og:description" content="<?php echo $facebook_description; ?>" /><?php } ?>
    </head>
    

    Finally, you can set facebook_description in each controller, by calling $this->document->setFacebookDescription('my description');.

    Example: in product.php controller you add

    <?php
    class ControllerProductProduct extends Controller {
    
        public function index() {
    
            // code...
            $this->document->setTitle($product_info['meta_title']);
            $this->document->setDescription($product_info['meta_description']);
    
            $this->document->setFacebookDescription($product_info['meta_description']);
    
            // the rest of the code...
        }
    }
    

    here you set the $product_info['meta_description'] as facebook description tag, however you could also use $product_info['name'] or other variable.

    Final note: you can change all the Opencart system classes with the vqmod.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题