get_posts を含めたコードをもっと簡潔にしたい
-
「Advanced Custom Field」プラグインを使用してはいますが、どちらかと言うとこちらのフォーラムの方がいいかと思い、投稿します。
カスタムタクソノミー「shop」を作成、「Advanced Custom Field」でカスタムフィールドを作成し店舗の情報を登録、店舗一覧ページを作成し、get_posts を使用して47都道府県に分けて表示させるようにしました。
表示自体はうまくいったのですが、PHP自体詳しくないのもあり、同じ表記を繰り返し書いてしまっています。もっと簡潔にできるのではないかと思い、どなたかお知恵を拝借したく存じます。カスタムフィールド「pref」で都道府県の店舗を絞り込んでいます。
<div class="area"><img src="<?php bloginfo('template_url'); ?>/images/shop/tab_hokkaido.gif" width="580" height="30" alt="北海道" /></div> <?php $posts = get_posts(array( 'numberposts' => 10, 'post_type' => 'shop', 'orderby' => 'meta_value', 'meta_key' => 'pref', 'order' => 'ASC', 'post_status' => 'publish', 'meta_query' => array( array( 'key' => 'pref', 'value' => '0101', ) ) )); if($posts){ echo '<div class="pref">北海道</div>'; foreach($posts as $post){ echo '<h3><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></h3>'; echo '<div class="box">'; if(get_field('main_image')) { echo '<p class="alignleft">'; $attachment_id = get_field('main_image'); $size = "thumbnail"; $image = wp_get_attachment_image_src( $attachment_id, $size ); $attachment = get_post( get_field('main_image') ); $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true); $image_title = $attachment->post_title; echo '<a href="'; echo get_permalink($post->ID); echo '"><img src="'; echo $image[0]; echo '" width="'; echo $image[1]; echo '" height="'; echo $image[2]; echo '" alt="'; echo $alt; echo '" title="'; echo $image_title; echo '" class="main_img" />'; echo '</a></p>'; }else{ } echo '<ul>'; if(get_field('zipcode')) { echo '<li>〒'; the_field('zipcode'); echo '</li>'; }else{ } if(get_field('address')) { echo '<li>'; the_field('address'); echo '<br />'; the_field('building'); echo '</li>'; }else{ } if(get_field('shop_tel')) { echo '<li>TEL:'; the_field('shop_tel'); if(get_field('shop_tel_type')=='chokutsu') { echo '(直通)'; } elseif(get_field('shop_tel_type')=='daihyo'){ echo '(代)'; }else{ } echo '</li>'; }else{ } echo '</ul>'; echo '</div>'; } }else{ } ?> <div class="area"><img src="<?php bloginfo('template_url'); ?>/images/shop/tab_touhoku.gif" width="580" height="30" alt="東北地方" /></div> <?php $posts = get_posts(array( 'numberposts' => 10, 'post_type' => 'shop', 'orderby' => 'meta_value', 'meta_key' => 'pref', 'order' => 'ASC', 'post_status' => 'publish', 'meta_query' => array( array( 'key' => 'pref', 'value' => '0201', ) ) )); if($posts){ echo '<div class="pref">青森県</div>'; foreach($posts as $post){ echo '<h3><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></h3>'; echo '<div class="box">'; if(get_field('main_image')) { echo '<p class="alignleft">'; $attachment_id = get_field('main_image'); $size = "thumbnail"; $image = wp_get_attachment_image_src( $attachment_id, $size ); $attachment = get_post( get_field('main_image') ); $alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true); $image_title = $attachment->post_title; echo '<a href="'; echo get_permalink($post->ID); echo '"><img src="'; echo $image[0]; echo '" width="'; echo $image[1]; echo '" height="'; echo $image[2]; echo '" alt="'; echo $alt; echo '" title="'; echo $image_title; echo '" class="main_img" />'; echo '</a></p>'; }else{ } echo '<ul>'; if(get_field('zipcode')) { echo '<li>〒'; the_field('zipcode'); echo '</li>'; }else{ } if(get_field('address')) { echo '<li>'; the_field('address'); echo '<br />'; the_field('building'); echo '</li>'; }else{ } if(get_field('shop_tel')) { echo '<li>TEL:'; the_field('shop_tel'); if(get_field('shop_tel_type')=='chokutsu') { echo '(直通)'; } elseif(get_field('shop_tel_type')=='daihyo'){ echo '(代)'; }else{ } echo '</li>'; }else{ } echo '</ul>'; echo '</div>'; } }else{ } ?>
以下、47都道府県分続きます。
何卒よろしくお願いいたします。
5件の返信を表示中 - 1 - 5件目 (全5件中)
5件の返信を表示中 - 1 - 5件目 (全5件中)
- トピック「get_posts を含めたコードをもっと簡潔にしたい」には新たに返信することはできません。