Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('wp_ajax_my_ajax_action', 'my_ajax_function');
- add_action('wp_ajax_nopriv_my_ajax_action', 'my_ajax_function');
- function my_ajax_function(){
- $q = new WP_Query(array(
- 'posts_per_page' => 10,
- 'post_type' => 'product'
- ));
- $html = '<ul>';
- while($q->have_posts()) : $q->the_post();
- $html .= '<li>'.get_the_title().'</li>';
- endwhile; wp_reset_query();
- $html .= '</ul>';
- echo $html;
- die();
- };
- function my_ajax_shortcode(){
- $html = '<button class="my_ajax_trigger">Test</button>
- <div id="info"></div>
- <script>
- jQuery(document).ready(function($){
- $(".my_ajax_trigger").on("click", function(){
- $.ajax({
- url: "'.admin_url("admin-ajax.php").'",
- data: {
- action: "my_ajax_action"
- },
- type: "POST",
- success: function(html){
- $("#info").append(html);
- }
- });
- });
- });
- </script>
- ';
- return $html;
- }
- add_shortcode('ajax_shortcode', 'my_ajax_shortcode');
Advertisement
Add Comment
Please, Sign In to add comment