カスタムフィールドを使ってのマルチループについて
-
カスタムフィールドの値を条件に2つの違う条件のループをindex.phpに表示させたいと考えています。
1つ目のループは上手く表示できるのですが、2つ目のループが上手く表示できません。
ただし、1つ目のループで表示したのアイテムを2つ目のループの条件に含めるとこのアイテムは表示できます。phpMyadminで表示できるアイテムとできないアイテムの中身を見比べましたが、カラムの値に差はありませんでした。<ul> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); $post_meta = get_post_meta(get_the_ID(),'recommend', true); if ($post_meta == 1) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else if ($post_meta == 2) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else if ($post_meta == 3) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else {} ?> <?php endwhile; ?> <?php else: ?> <?php wp_reset_query(); ?> <?php endif; ?> </ul> </li> <ul> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); if (get_post_meta($post->ID,'rank',true) == 10) { ?><li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else if ($post_meta == 11) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else if ($post_meta == 12) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else {} ?> <?php endwhile; ?> <?php else: ?> <?php wp_reset_query(); ?> <?php endif; ?> </ul>
何卒確認のほどよろしくお願いします。
-
若干ソースに誤りを見つけたので訂正しました。
<ul> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); $post_meta = get_post_meta(get_the_ID(),'recommend', true); if ($post_meta == 1) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else if ($post_meta == 2) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else if ($post_meta == 3) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else {} ?> <?php endwhile; ?> <?php else: ?> <?php wp_reset_query(); ?> <?php endif; ?> </ul> <ul> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); if ($post_meta == 10) { ?><li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else if ($post_meta == 11) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else if ($post_meta == 12) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li> <?php } else {} ?> <?php endwhile; ?> <?php else: ?> <?php wp_reset_query(); ?> <?php endif; ?> </ul>
ご回答ありがとうございます。早速以下の様に記載してみましたが、結果は変わりませんでした。
-
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php rewind_posts(); ?>
if ($post_meta == 10) {
?><li class=”loop_product_box”>“
class=”loop_shelf”><img src=”http://image.moshimo.com/item_image/<?php
echo get_post_meta($post->ID, ‘image_code’, true); ?>/1/l.jpg”
height=”200px” width=”200px” alt=”商品画像” title=””><?php the_title();
?>
<?php } else if ($post_meta == 11) { ?>
<li class=”loop_product_box”>“
class=”loop_shelf”><img src=”http://image.moshimo.com/item_image/<?php
echo get_post_meta($post->ID, ‘image_code’, true); ?>/1/l.jpg”
height=”200px” width=”200px” alt=”商品画像” title=””><?php the_title();
?>
<?php } else if ($post_meta == 12) { ?>
<li class=”loop_product_box”>“
class=”loop_shelf”><img src=”http://image.moshimo.com/item_image/<?php
echo get_post_meta($post->ID, ‘image_code’, true); ?>/1/l.jpg”
height=”200px” width=”200px” alt=”商品画像” title=””><?php the_title();
?>
<?php } else {} ?>
<?php endwhile; ?>
<?php else: ?>
<?php wp_reset_query(); ?>
<?php endif; ?>度々ありがとうございます。
<?php if (have_posts()) : ?>
の前に設置してみましたが、だめでしたので
後ろに設置してみましたが、結果は変わらずでした。<?php rewind_posts(); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php if ($post_meta == 10) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li>
横からすみません
wp_reset_query ここでいいのですか?
<?php if (have_posts()) : ?> ..... <?php else: ?> <?php wp_reset_query(); ?> <?php endif; ?>
【修正後のソース】
後ろのループでif ($post_meta == 10) {
の前に $post_meta の設定が必要では?
$post_meta = get_post_meta(get_the_ID(),'recommend', true); or $post_meta = get_post_meta(get_the_ID(),'rank',true); or $post_meta = get_post_meta(get_the_ID(),'*****',true); if ($post_meta == 10) {
nobitaさん
ありがとうございます。
wp_reset_queryは必要かと思って記載していたのですが、その場所が正しいかは分かりません。。。plute1234さn
ありがとうございます。
$post_metaの設定が抜けておりましたので記載したしました。
ご指摘ありがとうございます。
しかしまだ2つ目のループは表示されない状態となっています。<ul class="ranking_column ranking_border_column"> <?php rewind_posts(); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php $post_meta = get_post_meta($post->ID,'rank',true); ?> <?php if ($post_meta == 10) { ?> <li class="loop_product_box"><a href="<?php the_permalink() ?>" class="loop_shelf"><img src="http://image.moshimo.com/item_image/<?php echo get_post_meta($post->ID, 'image_code', true); ?>/1/l.jpg" height="200px" width="200px" alt="商品画像" title=""><?php the_title(); ?></a></li>
今わかっていることを書きます。現在のソースコードの状態で念のため確認しましたが、1つ目のループで読み込んだ投稿は2つ目のループでも問題なく表示出来る状態です。それ以外の投稿は表示できるものや表示できないものが混在している状態です。
すみません、以下の経緯で解決しました。
一応分かる範囲で原因を記載しておきます。カスタムフィールドの値はAdvanced Custom Fields v3.5.5を使用して入力していました。
PHPMyadminにて値を再度確認したところ、ループに表示されない投稿のみ、指定していた’rank’のmeta_keyに加えて、’_rank’というカラムが追加されていました。正常に表示されている投稿はこのカラムは存在していませんでしたので、この投稿を削除し、新たなpost_idで投稿し直したところ正常に表示されることを確認しました。今回は2つ目のループは9件しか表示しない為、全て手入力でポストを再投稿し直すことで解決とします。
皆様の迅速で丁寧なご回答誠にありがとうございました。
- トピック「カスタムフィールドを使ってのマルチループについて」には新たに返信することはできません。