在WordPress中,可以使用get_post_meta()
函数来调用自定义字段。这个函数需要一个$post_id作为参数,如果不提供$post_id,那么它将使用当前的post ID。
解决方案1:调用特定post的自定义字段
$post_id = 123; // 替换为你的post ID
$custom_field_name = 'your_custom_field_name'; // 替换为你的自定义字段名
$value = get_post_meta($post_id, $custom_field_name, true);
echo $value;
解决方案2:调用当前post的自定义字段
$custom_field_name = 'your_custom_field_name'; // 替换为你的自定义字段名
$value = get_post_meta(get_the_ID(), $custom_field_name, true);
echo $value;
解决方案3:循环调用所有自定义字段
$post_id = 123; // 替换为你的post ID
$custom_fields = get_post_custom($post_id);
foreach ($custom_fields as $key => $value) {
echo $key . ' => ' . $value[0] . '<br>';
}
解决方案4:使用get_post_meta()
函数获取多个值
$post_id = 123; // 替换为你的post ID
$custom_field_name = 'repeated_custom_field'; // 替换为你的自定义字段名
$values = get_post_meta($post_id, $custom_field_name, false);
foreach ($values as $value) {
echo $value . '<br>';
}
注意:在使用get_post_meta()
函数时,第三个参数的值决定了返回单个值还是返回所有值。如果设置为true
,则返回单个值,如果设置为false
,则返回所有值,即使只有一个值。