王彦国 發表於 2023-10-12 00:00:00

wordpress获取自定义字段get_post_meta函数使用介绍

<p>
<span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>wordpress可以设置自定义字段,方便扩展功能,wordpress利用巧妙的数据库表设计达到这一目的,posts表存放文章,页面和附件等,与之对应的postmeta表用来存储自定义的字段,采用post_id,key,value这样的设计来存放自定义字段的值。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>get_post_meta函数用法: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>get_post_meta($post_id, $key, $single); </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>该函数有3个基本参数: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>$post_id —— 所检索数据的文章的ID,使用 $post-&gt;ID 来获取文章的ID。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>$key —— 要检索的自定义字段名称 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>$single —— 这是一个布尔值,如果设置为 true ,将直接以字符串的形式返回字段的值;一个自定义字段可以填写多个值,如果设置为 false,将返回一个数组 array 来显示这多个值。 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>此函数定义在wordpress的post.php中: </span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
function get_post_meta($post_id, $key = '', $single = false) { <br>
return get_metadata('post', $post_id, $key, $single); <br>
} </p>
<p>
<br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>来看一个使用了wp_cache_set,wp_cache_get和get_post_meta函数的示例: </span></p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<p style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
&lt;?php <br>
$post_id = $post-&gt;ID; <br>
$post_views = wp_cache_get($post_id,'views'); <br>
if($post_views === false){ <br>
$post_views = get_post_meta($post_id, "views",true); <br>
if(!$post_views) $post_views = 0; <br>
} <br>
$post_views = $post_views + 1; <br>
wp_cache_set($post_id,$post_views,'views'); <br>
if($post_views%10 == 0){ <br>
update_post_meta($post_id, 'views', $post_views); <br>
} <br>
echo $post_views; <br>
?&gt; </p>
頁: [1]
查看完整版本: wordpress获取自定义字段get_post_meta函数使用介绍