contactform7のセレクトボックスにカスタム投稿のタイトルを自動取得する方法。 header.phpに以下を記述 記述箇所はjquery以下になるので、基本的にはwp_headより下になる。
if( is_page('10')): ?> <script type='text/javascript'> jQuery(function(){ <?php $my_query = new WP_Query('&post_type=recruit'); ?> <?php if ($my_query->have_posts()) : ?> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> jQuery('#select-title').append(jQuery('<option>').attr({ value: '<?php the_title(); ?>' }).text('<?php the_title(); ?>')); <?php endwhile; ?> <?php endif; ?> }); </script> <?php endif; ?>
軽い解説
header.php
最初の is_page('10') はページIDを指定。 contactform7のショートコードが記載されている場所になります。 ※例では固定ページに埋め込んでる場合
次にこのrecruitをカスタム投稿の任意のスラッグに変更。