admin管理员组文章数量:1390784
I am developing a website using WP CMS where I want to implement post title as drop-down list as well as when users select a drop-down title the content show below within reload. I am here. What will I do if I want to retrieve content by id?
function select_options(){ ?>
<form action="" method="POST">
<select name="count" id="selectId" >
<?php
$posts = new WP_Query(array('posts_per_page' => 3, 'post_type' => 'post'));
while($posts->have_posts()) : $posts->the_post(); ?>
<option id="selection" value="<?php echo get_the_ID(); ?>"><?php echo get_the_title(); ?></option>
<?php endwhile;
die()
?>
</select>
</form>
<?php
}
add_action('wp_ajax_select_option', 'select_options');
add_action('wp_ajax_nopriv_select_option', 'select_options');
Javascript:
jQuery(document).ready(function(){
jQuery.ajax({
//var count = $('#selection').val();
type: 'POST',
url: 'wp-admin/admin-ajax.php',
data:{
action: 'select_option'
//count: count;
},
dataType: 'html',
success: function(data){
jQuery('.content').html(data)
},
error: function(){
alert(fail)
}
})
});
I am developing a website using WP CMS where I want to implement post title as drop-down list as well as when users select a drop-down title the content show below within reload. I am here. What will I do if I want to retrieve content by id?
function select_options(){ ?>
<form action="" method="POST">
<select name="count" id="selectId" >
<?php
$posts = new WP_Query(array('posts_per_page' => 3, 'post_type' => 'post'));
while($posts->have_posts()) : $posts->the_post(); ?>
<option id="selection" value="<?php echo get_the_ID(); ?>"><?php echo get_the_title(); ?></option>
<?php endwhile;
die()
?>
</select>
</form>
<?php
}
add_action('wp_ajax_select_option', 'select_options');
add_action('wp_ajax_nopriv_select_option', 'select_options');
Javascript:
jQuery(document).ready(function(){
jQuery.ajax({
//var count = $('#selection').val();
type: 'POST',
url: 'wp-admin/admin-ajax.php',
data:{
action: 'select_option'
//count: count;
},
dataType: 'html',
success: function(data){
jQuery('.content').html(data)
},
error: function(){
alert(fail)
}
})
});
Share
Improve this question
edited Mar 26, 2017 at 18:47
MahdiY
4693 silver badges16 bronze badges
asked Mar 26, 2017 at 12:04
Wordpress StudentWordpress Student
117 bronze badges
1 Answer
Reset to default 1It's better to use the wp_ajax_ action in an init action.
In fact, wp_ajax_ action need to be accessable from anywhere. Use of the init action hook could make it work like it does.
Hope it works
本文标签: phpHow to make drop down post title
版权声明:本文标题:php - How to make drop down post title 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744753832a2623340.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论