编辑:我有一个自定义帖子类型的存档页面。此页面上显示的每篇文章都有通过高级自定义字段输入的关于该文章的信息,以及打开模式窗口的按钮(隐藏在循环中的div,用于打开php mailer表单)。在该模式窗口中,有一个下拉列表。
作为选项填充自定义帖子类型标题的下拉列表。它使用WP查询从Wordpress中提取这些标题。
CPT详细信息部分和模式打开按钮的代码
<div class="property-details">
<h2><?php the_title(); ?></h2>
<?php echo get_field(\'property_address\');?>
<!-- excerpt -->
<?php if( \'\' != get_the_excerpt() || \'\' != get_the_content() ) { ?>
<?php do_action(\'layers_before_list_post_content\'); ?>
<?php do_action(\'layers_list_post_content\'); ?>
<?php do_action(\'layers_after_list_post_content\'); ?>
<?php } ?>
</div>
<a href="#open-modal-form-<?php echo $post->ID; ?>" class="button inquire-button">Inquiry</a>
模态部分代码:
<div id="open-modal-form-<?php echo $post->ID; ?>" class="modal-window">
<div>
<a href="#" title="close" class="modal-close"><i class="eicon-close"></i></a>
<!-- I couldn\'t get contact form 7 to play nice, so I made my own form. Recipient Email is managed in inquiry.php. -->
<form action="<?php bloginfo(\'template_directory\'); ?>/inquiry.php" method="post">
<input type="text" name="fname" placeholder="First name">
<input type="text" name="lname" placeholder="Last name">
<?php
$args = array(
\'post_type\' => \'property\',
\'tax_query\' => array(
array(
\'taxonomy\' =>\'project_category\',
\'field\' => \'slug\',
\'terms\' => array(\'current-projects\',\'past-projects\')
)
),
);
$the_query = new WP_Query($args);
?>
<select name="propname">
<?php
if($the_query->have_posts()){
while($the_query->have_posts()) : $the_query->the_post(); ?>
<option><?php the_title(); ?></option>
<?php endwhile;
} wp_reset_query();
?>
</select>
<input type="email" name="email" placeholder="Email">
What type of lease are you looking for?
<input type="radio" name="lease-type" value="Rental" checked>Rental
<input type="radio" name="lease-type" value="Commercial">Commercial
<textarea name="comments" placeholder="Message"></textarea>
<input type="submit" value="Send Request">
</form>
<p><small>DB Services does not collect any personally identifiable information about you when you visit the Website unless you voluntarily provide this information, for example by contacting us through our email forms (including sending us queries or responding through the Website to our job postings.) Personal information collected in these cases may include your name, contact details, email address, telephone number and your resume.</small></p>
</div>
</div>
我如何才能在我当前所在的页面上(下拉列表中有一个CPT模式窗口)选择下拉列表中的一个?
最合适的回答,由SO网友:Sally CJ 整理而成
(修改后的答案)
如果我理解正确,请尝试以下方法:
定义$the_post
对于每个“属性”:
<?php $the_post = get_post(); ?>
<div class="property-details">
<h2><?php the_title(); ?></h2>
...
</div>
然后将下拉代码更改为:
<select name="propname">
<?php
$found = wp_list_filter( $the_query->posts, [ \'ID\' => $the_post->ID ] );
if ( empty( $found ) ) :
?>
<option selected><?php echo get_the_title( $the_post ); ?></option>
<?php
endif;
if($the_query->have_posts()){
while($the_query->have_posts()) : $the_query->the_post(); ?>
<option<?php selected( get_the_ID(), $the_post->ID ); ?>><?php the_title(); ?></option>
<?php endwhile;
} wp_reset_query();
?>
</select>