[英]Get URL parameter and pass to form select in Wordpress
I need to get a parameter from URL and select an option based on that parameter.我需要从 URL 和 select 获取一个基于该参数的选项。
This is the code that just worked until I have updated Contact Form 7 plugin:这是在我更新 Contact Form 7 插件之前有效的代码:
PHP: PHP:
if (isset($_GET['location'])) {
$parameter = $_GET['location'];
echo $parameter;
} else {
// Fallback behaviour goes here
}
JS: JS:
jQuery(document).ready(function($){
$('#location').find('option[data-url=<?php echo $parameter; ?>]').attr('selected','selected');
});
WP code: WP代码:
<select onchange="location=this.value;" id="location">
<option readonly>Select your location</option>
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => '8',
'order' => 'ASC',
'orderby' => 'ID'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<option value="<?php echo get_option('home'); ?>/meet-the-fleet/?location=<?php the_title(); ?>" data-url="<?php the_title(); ?>"><?php the_title(); ?></option>
<?php endwhile; ?>
<?php endif; ?>
</select>
All the above code should do the trick, but after that update is not working anymore, even if in this case is not used for CF7 plugin... is just for a standalone select.以上所有代码都应该可以解决问题,但是在该更新之后不再起作用,即使在这种情况下不用于 CF7 插件...仅适用于独立的 select。 It might be jQuery related?
可能与 jQuery 有关?
Thank you a lot!十分感谢!
Seems that the problem was when the jQuery was loaded.似乎问题出在加载 jQuery 时。 It needed to be loaded before my code, not after, buy I am not understanding why before the CF7 update has worked just fine...
它需要在我的代码之前加载,而不是之后,购买我不明白为什么在 CF7 更新之前工作得很好......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.