1

我在 WordPress 中有一个名为“thumb-url”的自定义字段,其中包含图像的确切位置。如果“thumb-url”包含图像的位置,我只想显示图像。

我从 if 语句开始,如果“thumb-url”自定义字段中有值,则回显photo 存在,否则它什么也不做。

<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 部分获得 ↑ ?

非常感激...

4

2 回答 2

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

?>
于 2009-12-08T17:52:20.823 回答
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" />';
于 2009-12-08T18:11:01.917 回答