2011-05-20 61 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";