从特定帖子获取自定义字段值

时间:2011-11-14 14:38:17

标签: php wordpress custom-fields

我在functions.php文件中使用此代码:

function get_custom_field_value($szKey, $bPrint = false) {
global $post;
$szValue = get_post_meta($post->ID, $szKey, true);
if ( $bPrint == false ) return $szValue; else echo $szValue;}

和我的HTML中的这个在我需要获取自定义字段时引用它:

<?php if ( function_exists('get_custom_field_value') ){
    get_custom_field_value('now_location', true);} ?>

但这只适用于我在帖子中使用它,因为它需要当前帖子的字段值。

如何从一个确切的帖子中获取字段值(或几个)? 我想这与帖子的ID有关,但我不知道要更改/添加到代码中。

1 个答案:

答案 0 :(得分:1)

正如@janw建议的那样,将post id作为参数传递以获取特定帖子的自定义字段是件好事。

function get_custom_field_value($szKey,$postId, $bPrint = false) {
$szValue = get_post_meta($postId, $szKey, true);
if ( $bPrint == false ) return $szValue; else echo $szValue;}
相关问题