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.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题