如何获取wordpress外循环的自定义栏目

  • 时间:2020-05-22 15:47:15
  • 分类:网络文摘
  • 阅读:136 次

自定义栏目可以为我们的文章和页面增加一些额外的元数据,但这些元数据只能在循环内部(Loop)显示,在循环(Loop)外部是看不到的。由于wordpress的所有数据都存储在MySQL数据库中,所以我们可以通过MySQL的查询来获取循环之外的文章自定义栏目值。这会是一个很有用的wordpress技巧。方法如下:

如何获取wordpress外循环的自定义栏目

添加如下代码到wordpress主题的任意位置:

  1. <?php
  2. global $wp_query;
  3. $postid = $wp_query->post->ID;
  4. echo get_post_meta($postid, 'CUSTOM-FIELD-NAME', true);
  5. ?>

只需替换“CUSTOM-FIELD-NAME”为你的自定义栏目名称即可。

原文:http://www.trickspanda.com/2014/02/display-value-specific-custom-field-wordpress

推荐阅读:
Hiring CGI Influencers Now a Trend for Brands: What it Means for  How to Create Online Courses on Your Blog  How Does Link Building Support Blog Growth?  What’s a Mukbang and Why It’s Not Really That Great  Content Ideas for New Blogs During the Pandemic  ‘Labeled For Reuse’ Gone: The Latest Disarming Change on Google   Blogging Magnates and Their Incredible Wealth  Join this Niche: Inspirational Charitable and Non-Profit Blogs  7 Passive Income Ideas to Complement Your Blog  Funny YouTuber Creates Device to Shoot Facemasks To Your Face 
评论列表
添加评论