简体   繁体   English

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

[英]Adding images depending on custom field data

I've got the PHP here for adding an image to a Wordpress post based on the data within a custom field called 'ecpt_shrating'. 我这里有PHP,可根据自定义字段“ ecpt_shrating”中的数据将图像添加到Wordpress帖子中。

When placed in the post PHP it simply shows up the raw code although the images are visible. 当放置在PHP中时,尽管图像可见,但它仅显示原始代码。

Bit of newbie, sorry, any help much appreciated. 有点新手,对不起,任何帮助深表感谢。

  $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'>

    }

You must ensure that your code is placed in <?php ?> (PHP tags). 您必须确保将代码放在<?php ?> (PHP标记)中。 Your code will not work as is: 您的代码将无法正常运行:

<?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">';
}?>

or: 要么:

<?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.'">';
}?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM