我在WordPress中有一个名为“thumb-url”的自定义字段,其中包含图像的确切位置。如果“thumb-url”包含图像的位置,我想仅显示图像。
如果“thumb-url”自定义字段中有值,则我会从if语句开始回显照片存在,否则它什么都不做。
<div class="excerpt">
<?php
$key = 'thumb-url';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo 'photo exists';
}
?>
现在,如果“thumb-url”中有值,那么这里的代码是我真正想要的if语句:
<img alt="<?php the_title() ?>" src="<?php if ( function_exists('get_custom_field_value') ){ get_custom_field_value('thumb-url', true); } ?>" align="absmiddle" height="62" width="62" class="writtenpostimg" />
如何在if语句的echo部分中获得↑?
非常感谢...
答案 0 :(得分:2)
假设您正在回复某些复制/粘贴说明的页面:
<div class="excerpt">
<?php
$key = 'thumb-url';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo htmlspecialchars('<img alt="<?php the_title() ?>" src="<?php if ( function_exists(\'get_custom_field_value\') ){ get_custom_field_value(\'thumb-url\', true); } ?>" align="absmiddle" height="62" width="62" class="writtenpostimg" />');
}
?>
答案 1 :(得分:0)
这逃脱了代码...其他评论实际上打印出“&lt; img ...&gt;”,所以这取决于你想要做什么。
您可以删除PHP标记并使用条件表达式:
<div class="excerpt">
<?php
$key = 'thumb-url';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo '<img alt="'.the_title().'" src="'.(function_exists('get_custom_field_value')?get_custom_field_value('thumb-url', true):'').'" align="absmiddle" height="62" width="62" class="writtenpostimg" />';
如果您实际使用变量,可能更容易理解:
$url = ""
if(function_exists('get_custom_field_value'))
$url = get_custom_field_value('thumb-url', true);
echo '<img alt="'.the_title().'" src="'.$url.'" align="absmiddle" height="62" width="62" class="writtenpostimg" />';