所以我使用Wordpress高级自定义字段,结果是:
<?php the_field('output'); ?>
输出是纯文本,可以是:一个、两个或三个。我需要用图像替换输出文本。
我试过这个例子:如果字符串。。。但这对我不起作用。我也试过在那个链接的底部举一个例子,但仍然没有起作用。有问题的是,他们使用了小比特不同的输出。有人能帮我吗?
您需要使用get_field(),而不是the_field()。
根据您的相关问题:
$items = array(
"one" => "one.png",
"two" => "two.png",
"three" => "three.png"
);
<img src="<?php echo $items[ get_field('output') ]; ?>" />
您最好使用选择字段,因为您不能强迫用户使用文本字段添加特定值。
http://www.advancedcustomfields.com/resources/field-types/select/
而不是像用户574632的答案那样做。
我不使用高级自定义字段,但如果它没有与the_field()
等效的get
函数(这会让我感到惊讶),那么您可以使用以下方法(显然是根据需要填写src
和alt
属性)。
<?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;
?>
根据我对WordPress的了解,像the_title()
和the_content()
这样的函数只是直接回显内容,但通常有一个相应的get_the_*
函数来返回值。CCD_ 8具有CCD_。因此,您可以在变量中捕获它,并在switch
语句中使用它:
<?php
$output = get_field('output');
switch ($output) {
case 'one':
// first image
break;
case 'two':
// second image
break;
case 'three':
// third image
break;
}