最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Set default option in dropdown of WP_Query?

programmeradmin1浏览0评论

Edit: I have an archive page for a Custom Post Type. Each article that appears on this page has information entered via Advanced Custom Fields about that article, and a button that opens a modal window (hidden div in the loop, made to open a php mailer form). Inside that modal window, is a dropdown.

The dropdown that populates Custom Post Type titles as an option. It uses WP Query to pull those titles from Wordpress.

code for the CPT details section and modal open button

<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>

code for the modal section:

<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>

How do I have it so that on the page I am currently on (with one of the CPT modal windows in the dropdown) is the one in the dropdown already selected?

Edit: I have an archive page for a Custom Post Type. Each article that appears on this page has information entered via Advanced Custom Fields about that article, and a button that opens a modal window (hidden div in the loop, made to open a php mailer form). Inside that modal window, is a dropdown.

The dropdown that populates Custom Post Type titles as an option. It uses WP Query to pull those titles from Wordpress.

code for the CPT details section and modal open button

<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>

code for the modal section:

<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>

How do I have it so that on the page I am currently on (with one of the CPT modal windows in the dropdown) is the one in the dropdown already selected?

Share Improve this question edited Aug 14, 2019 at 4:55 Xero1 asked Aug 13, 2019 at 22:39 Xero1Xero1 1491 gold badge1 silver badge5 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

(Revised answer)

If I understand it properly, try these:

  1. Define $the_post for each "property":

    <?php $the_post = get_post(); ?>
    <div class="property-details">
        <h2><?php the_title(); ?></h2>
        ...
    </div>
    
  2. Then change the drop-down code to:

    <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>
    
发布评论

评论列表(0)

  1. 暂无评论