質問

WordPressには、「Thumb-url」と呼ばれるカスタムフィールドがあり、画像の正確な位置が含まれています。したい それだけ 「Thumb-url」に画像の位置が含まれている場合は、画像を表示します。

私は反響するIFステートメントから始めます 写真が存在します 「Thumb-url」カスタムフィールドに値がある場合、それ以外の場合は何もしません。

<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ステートメントのエコー部分の内側にその↑を取得するにはどうすればよいですか?

とても有難い...

役に立ちましたか?

解決

何らかのコピー/貼り付けの手順について、ページにエコーしていると仮定します。

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

?>

他のヒント

これはコードを逃れます...他のコメントは実際に印刷します」u003Cimg...> 「それで、それはあなたがやろうとしていることに依存します。

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" />';
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top