根据自定义字段数据添加图像

时间:2013-02-11 22:18:30

标签: php image custom-fields

我在这里有PHP用于根据名为'ecpt_shrating'的自定义字段中的数据向Wordpress帖子添加图像。

当放置在PHP后,它只显示原始代码,尽管图像是可见的。

有点新手,抱歉,任何帮助都非常感激。

  $value = get_post_meta( get_the_ID(), 'ecpt_shrating', true );
if( $value == hell ) { <img src='/wp-content/uploads/2013/02/image1.gif'>

    } elseif( $value == poor ) { <img src='/wp-content/uploads/2013/02/image2.gif'>

    } elseif( $value == nice ) { <img src='/wp-content/uploads/2013/02/image3.gif'>

    } elseif( $value == angelic ) { <img src='/wp-content/uploads/2013/02/image4.gif'>

    } elseif( $value == heaven ) { <img src='/wp-content/uploads/2013/02/image5.gif'>

    }

1 个答案:

答案 0 :(得分:0)

您必须确保将代码放在<?php ?>(PHP标记)中。您的代码将无法正常工作:

<?php
$value = get_post_meta( get_the_ID(), 'ecpt_shrating', true );
$image = '';
if( $value == 'hell' ){
    echo '<img src="/wp-content/uploads/2013/02/image1.gif">';
} elseif( $value == 'poor' ) {
    echo '<img src="/wp-content/uploads/2013/02/image2.gif">';
} elseif( $value == 'nice' ) {
    echo '<img src="/wp-content/uploads/2013/02/image3.gif">';
} elseif( $value == 'angelic' ) {
    echo '<img src="/wp-content/uploads/2013/02/image4.gif">';
} elseif( $value == 'heaven' ) {
    echo '<img src="/wp-content/uploads/2013/02/image5.gif">';
}?>

或:

<?php
$value = get_post_meta( get_the_ID(), 'ecpt_shrating', true );
$image = '';
switch($value){
    case "hell":
        $image = 'image1.gif';
        break;
    case "poor":
        $image = 'image2.gif';
        break;
    case "nice":
        $image = 'image3.gif';
        break;
    case "angelic":
        $image = 'image4.gif';
        break;
    case "heaven":
        $image = 'image5.gif';
        break;

}
if(!empty($image)){
    echo '<img src="/wp-content/uploads/2013/02/'.$image.'">';
}?>