Wordpress metabox:如何获取具有多个值的字段的值?

问题描述

| 我正在使用此Wordpress Metabox框架:http://www.deluxeblogtips.com/p/meta-box-script-for-wordpress.html 这是代码
array(
        \'name\' => \'<strong>Robots Meta</strong>\',\'desc\' => \'\',\'id\' => $prefix . \'robot\',\'type\' => \'radio\',\'options\' => array(                       
            \'if\' => \'index,follow\',\'in\' => \'index,nofollow\',\'nf\' => \'noindex,\'nn\' => \'noindex,nofollow\'
        ),),
如何在模板中调用每个无线电值的值? 我尝试这样做,但它只会检查是否已设置:
$metas = get_post_meta(get_the_ID(),\'hiro_robot\',false);


foreach ($metas as $meta) {
    echo $meta;
}


if (in_array($val,$metas)) {
    echo \"$val is set\";
} else {
    echo \"$val is not set\";
}
    

解决方法

我喜欢使用一个简短的辅助函数来获取元值。
function c3m_get_field($key,$echo = FALSE) {
global $post;
$custom_field = get_post_meta($post->ID,$key,true);
if ($echo == FALSE) return $custom_field;
echo $custom_field;
}
随时使用此功能时,如果您想回显meta键的值,则可以使用:
c3m_get_field(\'key\',TRUE);
如果要返回值:
c3m_get_field(\'key\',FALSE);
另外在您的答案中,您无需使用
get_the_ID()
功能,只需使用
$post->ID
    ,在这里我自己弄清楚了,在这里是这样的:
$meta = get_post_meta(get_the_ID(),\'hiro_robot\',true); 

if (is_page() || is_single() && $meta == \'if\')
echo \'<meta name=\"robots\" content=\"index,follow\" />\'.\"\\n\";

elseif (is_page() || is_single() && $meta == \'in\')
echo \'<meta name=\"robots\" content=\"index,nofollow\" />\'.\"\\n\";

elseif (is_page() || is_single() && $meta == \'nf\')
echo \'<meta name=\"robots\" content=\"noindex,follow\" />\'.\"\\n\";

elseif (is_page() || is_single() && $meta == \'nn\')
echo \'<meta name=\"robots\" content=\"noindex,nofollow\" />\'.\"\\n\";
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...