wordpressでpost取得でハマった事。
いきなりWPへの組み込み修正頼まれてハマった事。。
やりたかったことは記事を複数ID取得して、View側で回して表示したかった。
get_header(); $companyList = get_posts('include=' . implode(",", $sendForm['company']));
<?php foreach($companyList as $post ) : ?> <?php setup_postdata($post); ?> <?php the_title(); ?> <input type="hidden" name="sendForm[company][]" value="<?php the_ID(); ?>" /> <br> <?php endforeach; ?>
やらかしたことは以下。間違い!
<?php foreach($companyList as $company ) : ?> <?php setup_postdata($company); ?> <?php the_title(); ?> <input type="hidden" name="sendForm[company][]" value="<?php the_ID(); ?>" /> <br> <?php endforeach; ?>
$post上書きしてやんなきゃダメってのをまったく知らなくて、でthe_titleで画面のタイトル(?)出てくるわ、明らかにthe_IDが違うわで頭抱えてた。。
$postを上書きしてやらなきゃだめなのね…
ところで、なんでこんなやり方なんだ?
ちなみに参考にしたコードは以下。
<?php if ( have_posts() ) : query_posts('category_name=さんぷる'); ?> <?php $count = 1 ;?> <?php while (have_posts()) : the_post(); ?> <?php $key1='banarfield'; $key2='urlfield'; $key3='campany_email'; // $key4='star'; // $key5='price_star'; $value1= get_post_meta($post->ID, $key1, true); $value2= get_post_meta($post->ID, $key2, true); $value3= get_post_meta($post->ID, $key3, true); // $value4= get_post_meta($post->ID, $key4, true); // $value5= get_post_meta($post->ID, $key5, true); ?> <td class="checkbox-column"> <?php if($sendForm['company'] == null || !in_array($post->ID, $sendForm['company'])) : ?> <input type="checkbox" value="<?php the_ID(); ?>" name="sendForm[company][]" id="<?php the_ID(); ?>"/> <?php else : ?> <input type="checkbox" value="<?php the_ID(); ?>" name="sendForm[company][]" id="<?php the_ID(); ?>" checked="checked"/> <?php endif; ?> </td> <td class="banner-column"><label for="<?php the_ID(); ?>"> <?php if ($value1) :?> <img src="<?echo $value1?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?> " width="100" height="40" /> <?php else :?> <img src="/wp-content/uploads/bana_dummy-mini.jpg" alt="<?php the_title(); ?><?php echo '" title="'; ?><?php the_title(); ?>" width="100" height="40" /> <?php endif ?> <br> <?php the_title(); ?></label> </td> <?php if ($count%5 == 0): ?> </tr> <tr> <?php endif ?> <!-- /post --> <?$count++?> <?php endwhile;?> <?php wp_reset_query(); endif; ?>