カテゴリーページ(アーカイブ)で複数ループ
WordPress初心者&PHP知識ほぼゼロ…..なので、ハマった期間は軽く3日超。涙
忘れないように、メモ書き!
1、通常の「投稿(post)」を利用して「キャバクラ各店舗ページ」
2、カスタム投稿タイプで「ドレスショップ(dressshop)」を作成
この二つを通常のカテゴリーを利用した「エリア=地域」でカテゴリー分け。
テンプレート「アーカイブ」を利用した各カテゴリー(各エリア)ページには、
上部に1、
下部に2、
を表示されるようにする。
つまり、カテゴリー「上野」のカテゴリーページの
上の段に「上野のキャバクラ一覧」※通常の投稿(post)
下の段に「上野のドレスショップ一覧」※カスタム投稿タイプ(dressshop)
この場合、
複数のループが必要になるらしい。
Magicalog | マジカルリミックスエンジニアブログ様の
WordPressで今見ているカテゴリーのIDを取得し、query_postsに代入(改) | Magicalog
を参考にさせて頂き、下記のように記述。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <p><a href= "<?php the_permalink() ?>" ><?php the_title(); ?></a></p> <?php endwhile ; ?> <!--カスタム投稿タイプshop一覧--> <? foreach ((get_the_category()) as $cat ) { $cat_id = $cat ->cat_ID ; break ; } $query = 'cat=' . $cat_id . '&showposts=1' ; query_posts( $query ) ; ?> <?php global $post ; $myposts = get_posts( "category=$cat_id&post_type=shop&order=asc&numberposts=40" ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <p><a href= "<?php the_permalink(); ?>" title= "<?php the_title(); ?>の詳細へ" ><?php the_title(); ?></a></p> <!--/カスタム投稿タイプshop一覧--> <?php else : ?> <p>キャバクラ店舗の登録がありません</p> <!--直接応募店舗がない場合のカスタム投稿タイプshop一覧--> <? foreach ((get_the_category()) as $cat ) { $cat_id = $cat ->cat_ID ; break ; } $query = 'cat=' . $cat_id . '&showposts=1' ; query_posts( $query ) ; ?> <?php global $post ; $myposts = get_posts( "category=$cat_id&post_type=shop&order=asc&numberposts=40" ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <p><a href= "<?php the_permalink(); ?>" title= "<?php the_title(); ?>の詳細へ" ><?php the_title(); ?></a></p> <!--/直接応募店舗がない場合の紹介・派遣先店舗の一覧--> <?php endif ; ?> |
狙い通りの表示に……と思いきや、なんか変。。
————
Magicalog | マジカルリミックスエンジニアブログ様の
WordPressで今見ているカテゴリーのIDを取得し、query_postsに代入(改) | Magicalog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <? foreach ((get_the_category()) as $cat ) { $cat_id = $cat ->cat_ID ; break ; } $query = 'cat=' . $cat_id . '&showposts=1' ; query_posts( $query ) ; ?> <dl class = "news" > <?php global $post ; $myposts = get_posts( "category=$cat_id&post_type=shop&order=asc&numberposts=40" ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <dt style= "text-align:center;" ><a href= "<?php the_permalink(); ?>" title= "<?php the_title(); ?>の詳細へ" ><?php echo post_custom( "店舗名アルファベット" ); ?></a></dt> <dd><?php the_title(); ?>【<?php echo get_the_term_list( $post ->ID, 'type' , '' , '・' ); ?>】</dd> <?php endforeach ; ?> </dl> |
ソースコード全体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <h2><a href= "<?php the_permalink() ?>" rel= "bookmark" title= "Permanent Link to <?php the_title(); ?>" ><?php $cat = get_the_category(); $cat = $cat [0]; { echo $cat ->cat_name; } ?><?php echo post_custom( "店舗名アルファベット" ); ?>(<?php echo post_custom( "店舗名カナ" ); ?>)</a></h2> <div class = "contents" > <p class = "left" ><img src= "http://night-arcs.com/data/<?php echo post_custom(" 店舗コード "); ?>/top-pc.jpg" alt= "<?php $cat = get_the_category(); $cat = $cat[0]; { echo $cat->cat_name; } ?><?php echo post_custom(" 店舗名アルファベット "); ?>(<?php echo post_custom(" 店舗名カナ "); ?>)店内画像トップ" width= "234" height= "176" /></p><p><br /><?php echo post_custom( "テキスト1" ); ?></p> </div> <?php endwhile ; ?> <!--カスタム投稿タイプshop一覧--> <h2><?php single_cat_title(); ?>エリアの「紹介・派遣」先店舗の一覧</h2> <div class = "contents" > <? foreach ((get_the_category()) as $cat ) { $cat_id = $cat ->cat_ID ; break ; } $query = 'cat=' . $cat_id . '&showposts=1' ; query_posts( $query ) ; ?> <dl class = "news" > <?php global $post ; $myposts = get_posts( "category=$cat_id&post_type=shop&order=asc&numberposts=40" ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <dt style= "text-align:center;" ><a href= "<?php the_permalink(); ?>" title= "<?php the_title(); ?>の詳細へ" ><?php echo post_custom( "店舗名アルファベット" ); ?></a></dt> <dd><?php the_title(); ?>【<?php echo get_the_term_list( $post ->ID, 'type' , '' , '・' ); ?>】</dd> <?php endforeach ; ?> </dl> </div> <!--/カスタム投稿タイプshop一覧--> <?php posts_nav_link_keni(); ?> <?php else : ?> <h2>店舗がありません</h2> <div class = "contents" > <?php single_cat_title(); ?>エリアに直接応募出来る店舗はございません。 <p><?php printf( __( 'Back to <a href="%1$s">%2$s</a>' , 'keni' ), get_bloginfo( 'url' ), get_bloginfo( 'name' )); ?></p> </div> <!--直接応募店舗がない場合のカスタム投稿タイプshop一覧--> <h2><?php single_cat_title(); ?>エリアの「紹介・派遣」先店舗の一覧</h2> <div class = "contents" > <? foreach ((get_the_category()) as $cat ) { $cat_id = $cat ->cat_ID ; break ; } $query = 'cat=' . $cat_id . '&showposts=1' ; query_posts( $query ) ; ?> <dl class = "news" > <?php global $post ; $myposts = get_posts( "category=$cat_id&post_type=shop&order=asc&numberposts=40" ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <dt style= "text-align:center;" ><a href= "<?php the_permalink(); ?>" title= "<?php the_title(); ?>の詳細へ" ><?php echo post_custom( "店舗名アルファベット" ); ?></a></dt> <dd><?php the_title(); ?>【<?php echo get_the_term_list( $post ->ID, 'type' , '' , '・' ); ?>】</dd> <?php endforeach ; ?> </dl> </div> <!--/直接応募店舗がない場合の紹介・派遣先店舗の一覧--> <?php endif ; ?> |
これだけおさえる ネットでバイト様の
ワードプレスで現在のカテゴリーページのIDを取得する方法
1 2 3 4 | <? $categoryname = single_cat_title( '' ,false); $categoryid = get_cat_ID( $categoryname ); ?> |
これで、カテゴリー名⇒カテゴリーIDを取得して、
1 2 3 4 5 6 7 8 9 10 11 | <? $categoryname = single_cat_title( '' ,false); $categoryid = get_cat_ID( $categoryname ); ?> <dl class = "news" > <?php global $post ; $myposts = get_posts( "category=$categoryid&post_type=shop&numberposts=40" ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <dt style= "text-align:center;" ><a href= "<?php the_permalink(); ?>" title= "<?php the_title(); ?>" ><?php echo post_custom( "店舗名アルファベット" ); ?></a></dt> <dd><?php the_title(); ?>【<?php echo get_the_term_list( $post ->ID, 'type' , '' , '・' ); ?>】</dd> <?php endforeach ; ?> </dl> |
こうしたら上手くいきました!
全体のソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <h2><a href= "<?php the_permalink() ?>" rel= "bookmark" title= "Permanent Link to <?php the_title(); ?>" ><?php $cat = get_the_category(); $cat = $cat [0]; { echo $cat ->cat_name; } ?><?php echo post_custom( "店舗名アルファベット" ); ?>(<?php echo post_custom( "店舗名カナ" ); ?>)</a></h2> <div class = "contents" > <p class = "left" ><img src= "http://night-arcs.com/data/<?php echo post_custom(" 店舗コード "); ?>/top-pc.jpg" alt= "<?php $cat = get_the_category(); $cat = $cat[0]; { echo $cat->cat_name; } ?><?php echo post_custom(" 店舗名アルファベット "); ?>(<?php echo post_custom(" 店舗名カナ "); ?>)店内画像トップ" width= "234" height= "176" /></p><p><br /><?php echo post_custom( "テキスト1" ); ?></p> </div> <?php endwhile ; ?> <?php posts_nav_link_keni(); ?> <!--カスタム投稿タイプshop一覧--> <p><br /></p> <h2><?php single_cat_title(); ?>エリアの「紹介・派遣」先店舗の一覧</h2> <div class = "contents" > <? $categoryname = single_cat_title( '' ,false); $categoryid = get_cat_ID( $categoryname ); ?> <dl class = "news" > <?php global $post ; $myposts = get_posts( "category=$categoryid&post_type=shop&numberposts=40" ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <dt style= "text-align:center;" ><a href= "<?php the_permalink(); ?>" title= "<?php the_title(); ?>の詳細へ" ><?php echo post_custom( "店舗名アルファベット" ); ?></a></dt> <dd><?php the_title(); ?>【<?php echo get_the_term_list( $post ->ID, 'type' , '' , '・' ); ?>】</dd> <?php endforeach ; ?> </dl> </div> <!--/カスタム投稿タイプshop一覧--> <?php else : ?> <h2>店舗がありません</h2> <div class = "contents" > <?php single_cat_title(); ?>エリアに直接応募出来る店舗はございません。 </div> <!--カスタム投稿タイプshop一覧--> <p><br /></p> <h2><?php single_cat_title(); ?>エリアの「紹介・派遣」先店舗の一覧</h2> <div class = "contents" > <? $categoryname = single_cat_title( '' ,false); $categoryid = get_cat_ID( $categoryname ); ?> <dl class = "news" > <?php global $post ; $myposts = get_posts( "category=$categoryid&post_type=shop&numberposts=40" ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <dt style= "text-align:center;" ><a href= "<?php the_permalink(); ?>" title= "<?php the_title(); ?>の詳細へ" ><?php echo post_custom( "店舗名アルファベット" ); ?></a></dt> <dd><?php the_title(); ?>【<?php echo get_the_term_list( $post ->ID, 'type' , '' , '・' ); ?>】</dd> <?php endforeach ; ?> </dl> </div> <!--/カスタム投稿タイプshop一覧--> <?php endif ; ?> |
カテゴリー名やID、親カテゴリーIDなどの取得コード
※けどこの方法もこの記事のケースではダメでした
1 2 3 4 5 6 7 8 9 10 | <?php /* 現在のカテゴリ-の取得 */ $cat_now = get_the_category(); $cat_now = $cat_now [0]; /*親カテゴリーのID取得*/ $parent_id = $cat_now ->category_parent; /*現在のカテゴリーID/カテゴリー名取得*/ $now_id = $cat_now ->cat_ID; /* カテゴリID */ $now_name = $cat_now ->cat_name; /* カテゴリ名 */ ?> |
↑OpenMediaLaboratory Blog様の
WordPressでカテゴリID/カテゴリ名を取得する | OpenMediaLaboratory参考