2013-12-10 25 views
0

這樣的IM使用Wordpress先進的自定義字段替換字符串,outpit是這樣的:WordPress的PHP - 用繩子

<?php the_field('output'); ?> 

輸出是純粹的文字,也可以是:一個,兩個或三個。我需要用圖像替換輸出文字。

我試過這個例子:if string...但它不適合我。我也嘗試了swithc例子的鏈接底部,但仍然無法正常工作。 Probobly couse他們使用litlle位不同的輸出。有人能幫我嗎?

回答

4

您需要使用get_field()而不是the_field()。

根據你的鏈接的問題:

$items = array(
    "one" => "one.png", 
    "two" => "two.png", 
    "three" => "three.png" 
); 


<img src="<?php echo $items[ get_field('output') ]; ?>" /> 
+0

working,THX !!!! – Dreadlord

-2

據我瞭解關於WordPress,像the_title()the_content()功能只不過是迴應內容進行直接的,但通常有一個相應的get_the_*函數返回值來代替。 the_field()get_field()。所以,你可以捕捉它的一個變量,在switch語句中使用它:

<?php 
$output = get_field('output'); 

switch ($output) { 
    case 'one': 
     // first image 
    break; 
    case 'two': 
     // second image 
    break; 
    case 'three': 
     // third image 
    break; 
} 
0

我不使用先進的自定義字段,但如果它有the_field()沒有等效get功能(這讓我感到吃驚),那麼你可以使用下面的方法(如需要在srcalt屬性明顯充盈)。

<?php 
ob_start(); 
the_field('output'); 
$result = ob_get_clean(); 

switch($result) : 

    case 'one' : 
     echo '<img src="" alt="" />'; 
     break; 
    case 'two' : 
     echo '<img src="" alt="" />'; 
     break; 
    case 'three' : 
     echo '<img src="" alt="" />'; 
     break; 

endswitch; 
?>