【问题标题】:Keeping the value of a select from wordpress loop保持从 wordpress 循环中选择的值
【发布时间】:2018-01-09 23:01:21
【问题描述】:

我正在寻找一种方法来保留从 WordPress 发布循环中创建的选项列表中的选择:

<?php 
$args = array( 'post_type' => 'office_locations', 'posts_per_page' => -1, 'order_by' => 'title', 'order' => 'ASC'  );
$loop = new WP_Query( $args ); ?>

<select style="width: 100%;"  name="selectedValue" onchange="this.form.submit()">

<option disabled>Select an office location...</option> // This is disabled
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<option><?php echo get_the_title();?></option>
<?php endwhile; ?>

</select>

因此,如果用户进行了选择,我已将其设置为在选择后发布,有没有办法让选择在发生这种情况后保留在该选项上?

【问题讨论】:

    标签: php jquery wordpress


    【解决方案1】:

    当然,检查POST的值是否与select的值匹配,然后设置为selected:

    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <option<?= (isset($_POST['selectedValue']) && $_POST['selectedValue'] == get_the_title() ? ' selected' : null) ?>><?php echo get_the_title();?></option>
    <?php endwhile; ?>
    

    【讨论】:

    • 这是完美的:)
    猜你喜欢
    • 2021-05-07
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 2016-03-22
    • 2016-08-11
    • 2022-01-12
    • 2021-04-04
    • 1970-01-01
    相关资源
    最近更新 更多