2011-05-20 62 views
0

即时通讯使用这个WordPress Metabox框架:http://www.deluxeblogtips.com/p/meta-box-script-for-wordpress.htmlWordpress metabox:如何获得具有多个值的字段值?

这里是代码

array(
     'name' => '<strong>Robots Meta</strong>',   
     'desc' => '',       
     'id' => $prefix . 'robot',    
     'type' => 'radio',      
     'options' => array(      
      'if' => 'index, follow', 
      'in' => 'index, nofollow', 
      'nf' => 'noindex, follow', 
      '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"; 
} 

回答

0

我喜欢用用于获取元值的简短帮助函数。

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; 
} 

使用时要呼应元键的值这个任何时候,你可以使用:

c3m_get_field('key', TRUE); 

如果你想返回值:

c3m_get_field('key', FALSE); 

而且在回答你不需要用get_the_ID()功能就用$post->ID

0

这里我理解了它自己,这是这样的:

$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";