问题描述
|
我正在使用此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\";