2013-12-10 22 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; 
?>