Guest User

Untitled

a guest
Oct 23rd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. //Enqueue custom scripts
  2. function my_adding_scripts() {
  3.  
  4.  
  5. wp_register_script('gg-load', '/wp-content/plugins/gg-load.js', array('jquery'),'1.1', true);
  6. wp_localize_script( 'gg-load', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
  7. wp_enqueue_script('gg-load');
  8.  
  9. }
  10.  
  11. add_action('wp_enqueue_scripts', 'my_adding_scripts');
  12.  
  13. //Process AJAX data
  14. add_action( 'wp_ajax_gg_query_posts', 'gg_query_posts' );
  15. add_action( 'wp_ajax_nopriv_gg_query_posts', 'gg_query_posts' );
  16.  
  17. function gg_query_posts() {
  18.  
  19. $response = array();
  20.  
  21. $slug = $_REQUEST["slug"];
  22.  
  23. $args = array(
  24. 'tag' => "$slug",
  25. 'orderby' => "title",
  26. 'order' => "ASC",
  27. );
  28.  
  29. $the_query = new WP_Query ($args);
  30.  
  31. if ( $the_query->have_posts() ) {
  32.  
  33. while ( $the_query->have_posts() ) {
  34.  
  35. $the_query->the_post();
  36.  
  37. $response->status = true;
  38.  
  39. $response->query = $the_query;
  40.  
  41. }
  42.  
  43. } else {
  44.  
  45. $response->status = false;
  46.  
  47. $response->message = esc_attr__( 'No posts were found' );
  48.  
  49. }
  50.  
  51. wp_reset_postdata();
  52. die( json_encode( $response ) );
  53.  
  54. }
  55.  
  56. jQuery(document).ready( function() {
  57.  
  58. jQuery("ul.gg-menu>li").click( function(e) {
  59. e.preventDefault();
  60. slug = jQuery(this).attr("data-slug"); //Passes slug to the PHP script for use in the WP_Query
  61.  
  62. jQuery.ajax({
  63. type : "post",
  64. dataType : "json",
  65. url : myAjax.ajaxurl,
  66. data : {action: "gg_query_posts", slug: slug},
  67. error: function(data) {
  68. console.log(data, "error");
  69. },
  70. success: function(data) {
  71. console.log(data, "success");
  72. //We work with the post data here
  73. }
  74. })
  75.  
  76. })
  77.  
  78. })
Add Comment
Please, Sign In to add comment