Reputation: 413
So im using Wordpress advanced custom fields, outpit is this:
<?php the_field('output'); ?>
Output is pure text, it can be: one, two or three. I need to replace output text with image.
I tried this example: if string... but its not working for me. I also tried swithc example on the bottom of that link but still not working. Probobly couse they use litlle bit diferent output. Can someone help me?
Upvotes: 0
Views: 503
Reputation: 12047
I don't use Advanced Custom Fields, but if it has no equivalent get
function for the_field()
(which would surprise me), then you could use the following method (obviously filling in the src
and alt
attributes as required).
<?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;
?>
Upvotes: 0
Reputation: 39389
From what I understand about WordPress, functions like the_title()
and the_content()
just echo the content out directly, but usually have a corresponding get_the_*
function that returns the value instead. the_field()
has get_field()
. So you can capture it in a variable and use it in a switch
statement:
<?php
$output = get_field('output');
switch ($output) {
case 'one':
// first image
break;
case 'two':
// second image
break;
case 'three':
// third image
break;
}
Upvotes: -2
Reputation: 12665
You'd better use a select field because you cannot force the users to add specific values with a textfield.
http://www.advancedcustomfields.com/resources/field-types/select/
And than do it like user574632's answer.
Upvotes: 0
Reputation: 20469
You need to use get_field() rather that the_field().
As per your linked question:
$items = array(
"one" => "one.png",
"two" => "two.png",
"three" => "three.png"
);
<img src="<?php echo $items[ get_field('output') ]; ?>" />
Upvotes: 4