2009-12-08 105 views
1

我在WordPress中有一個名爲「thumb-url」的自定義字段,其中包含圖像的確切位置。我只想如果「thumb-url」包含圖像的位置,則顯示圖像。回聲包含PHP的字符串(在WordPress中)

如果在「thumb-url」自定義字段中存在值,則我會從if語句開始回聲照片存在否則它什麼也不做。

<div class="excerpt"> 
<?php 
$key = 'thumb-url'; 
$themeta = get_post_meta($post->ID, $key, TRUE); 
if($themeta != '') { 
echo 'photo exists'; 
} 
?> 

現在,這裏是我真正想要的上方,如果回聲聲明,如果有在「拇指URL」的值代碼:

<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語句?

非常感謝......

+0

我可以把IMG + PHP標記到一個新的.php文件幷包含php文件? – 2009-12-08 17:47:27

+0

你......對我而言......儘管它不是超高效的,但它現在起作用。 – 2009-12-08 17:50:34

回答

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

?> 
+0

工程。謝謝你傑格 – 2009-12-08 18:08:25

0

該逃逸代碼...其他評論實際打印出「< IMG ... >」,所以這取決於你想要做什麼。

您可以刪除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" />';