duanhunlou7051 2011-07-12 08:18
浏览 27
已采纳

如何从模板添加元标记?

I'm developing a website using WordPress as my Framework. The templates are highly customized and really doesn't use much of Wordpress's features. I just use it for registering users and publish a few articles here and there.

One of the templates is a product template. In this template I retrieve product information based on product ID sent in the URL.

What I want to do, is to take info from this product and add it to my <meta> tags.

According to Wordpress documentation, headers are loaded before templates.

So, how can I add meta tag information based on data loaded in my template?

Do I have to query the database "a second time" in the header in order to retrieve the data wanted?

I know Wordpress has some functionality for this, since article titles can be retrieved in the header file. But I'm not sure how I can take advantage of this.

  • 写回答

1条回答 默认 最新

  • du9826 2011-07-12 10:56
    关注

    Even though header is loaded before template, few global variable are available even before header is loaded and $post is one of them which hold post data + metadata.

    So you can access post meta in header as well.

    global $post;
    echo get_post_meta($post->ID, 'ur_meta_key', true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致