
>
Wordpress > wordpress カスタムフィールドテンプレートで項目に記入のない場合は非表示
カスタムフィールドテンプレートで項目に画像やテキストが入力されない場合は非表示にする方法
1.パターン1(画像の場合)
<?php
$files = get_post_meta($post->ID, サブ画像1, false);
foreach($files as $file):
$file = wp_get_attachment_url($file);
if ( !empty( $file ) ) :
?>
<?php
$image = wp_get_attachment_image_src( get_post_meta($post->ID, 'サブ画像1', true),'full' );
?>
<a href="<?php echo $image[0];?>" rel="lightbox" />
<img src="<?php echo $image[0]; ?>" height="110px" alt="<?php echo $alt; ?>" title="<?php echo $title; ?>"></a>
<?php
endif;
endforeach;
?>
2.パターン2(テキストの場合)
<table>
<?php
$metakeys = array('規模・構造' );
foreach( $metakeys as $key ) :
$value = get_post_meta( $post->ID, $key, true );
$value = esc_html( $value ); // WP2.8+
// $value = wp_specialchars( $value );
if ( !empty( $value ) ) :
?>
<tr>
<th>設計</th>
<td><?php echo get_post_meta($post->ID,"規模・構造",true); ?></td>
</tr>
<?php
endif;
endforeach;
?>
</table>