wordpressのカスタムフィールドで条件分岐したいです。
プラグインはAdvanced Custom Fieldsを使っています。
内容としましては動画のフィールドに項目がある場合は動画を表示させて、動画が無い場合は画像を表示させたいです。
どんな方法があるでしょうか?
<div class="clearfix"> <?php query_posts( $args ); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="works"> <?php $youtube = get_post_meta($post->ID, 'youtube', true); ?> <?php if(!empty($youtube)):?> <div class="VideoWrapper"> <?php echo $embed_code = wp_oembed_get( get_field('youtube') ); ?> </div> <?php else:?> <div class="worksImg"> <img src="<?php echo $img['url']; ?>" alt="<?php echo $img['alt']; ?>" /> </div> <?php endif; ?> <div class="worksIco"> <h3><?php echo post_custom('crient'); ?></h3> <p><?php echo post_custom('content'); ?></p> </div> </div> <?php endwhile; ?> <?php else : ?> <h1>記事がありません</h1> <p>表示する記事はありませんでした。</p> <?php endif; ?> </div><!-- /clearfix -->
追記のコード↓
<?php // ループ条件を設定 $args = array( 'post_type' => array ('graphic','video','web'), /* 投稿タイプを指定 */ 'paged' => get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1, 'posts_per_page' => 10, // 表示件数 'order' => 'date', 'order' => 'DESC', ); ?> <div class="clearfix"> <?php query_posts( $args ); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="works"> <?php $youtube = get_post_meta($post->ID, 'youtube', true); ?> <?php if(!empty($youtube)):?> <div class="VideoWrapper"> <?php echo $embed_code = wp_oembed_get( get_field('youtube') ); ?> </div> <?php else:?> <?php $img = get_field('img'); if( !empty($img) ): ?> <div class="worksImg"> <img src="<?php echo $img['url']; ?>" alt="<?php echo $img['alt']; ?>" /> </div> <?php endif; ?> <div class="worksIco"> <h3><?php echo post_custom('crient'); ?></h3> <p><?php echo post_custom('content'); ?></p> </div> </div> <?php endwhile; ?> <?php else : ?> <h1>記事がありません</h1> <p>表示する記事はありませんでした。</p> <?php endif; ?> </div><!-- /clearfix -->
回答1件
あなたの回答
tips
プレビュー
2017/04/24 07:12
2017/04/24 07:13
2017/04/24 07:52
2017/04/24 08:17
2017/04/24 08:36
2017/04/24 08:52
2017/04/24 09:31
2017/04/24 09:46
2017/04/24 10:06