Advertisement
dgpope

wordpress functions for displaying content

Mar 9th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.59 KB | None | 0 0
  1. <?php
  2. // Script for getting posts
  3. function ajax_filter_get_posts( $taxonomy ) {
  4.  
  5.   // Verify nonce
  6.   if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
  7.     die('Permission denied');
  8.  
  9.   $taxonomy = $_POST['taxonomy'];
  10.  
  11.   // WP Query
  12.   $args = array(
  13.     'glossary_category' => $taxonomy,
  14.     'post_type' => 'glossary_item',
  15.     'posts_per_page' => -1,
  16.   );
  17.  
  18.   // If taxonomy is not set, remove key from array and get all posts
  19.   if( !$taxonomy ) {
  20.     unset( $args['cat'] );
  21.   }
  22.  
  23.   $query = new WP_Query( $args );
  24.  
  25.   if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
  26.  
  27.     echo '<h2>'.the_title().'</h2>';
  28.     $output = the_content();
  29.  
  30.     $result = 'success';
  31.  
  32.   endwhile; else:
  33.     $output = '<h2>No posts found</h2>';
  34.     $result = 'fail';
  35.   endif;
  36.  
  37.   $response = json_encode($output);
  38.   echo $response;
  39.  
  40.   die();
  41. }
  42.  
  43. add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
  44. add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
  45.  
  46.  
  47. // Script for getting terms
  48. function ajax_filter_get_terms( $taxonomy ) {
  49.  
  50.   // Verify nonce
  51.   if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
  52.     die('Permission denied');
  53.  
  54.   $taxonomy = $_POST['taxonomy'];
  55.  
  56.   // WP Query
  57.   $args = array(
  58.     'encyclopedia_category' => $taxonomy,
  59.     'post_type' => 'encyclopedia_item',
  60.     'posts_per_page' => -1,
  61.   );
  62.  
  63.   // If taxonomy is not set, remove key from array and get all posts
  64.   if( !$taxonomy ) {
  65.     unset( $args['encyclopedia_category'] );
  66.   }
  67.  
  68.   $query = new WP_Query( $args );
  69.    ?> <ul><?php
  70.   if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
  71. ?>
  72.  
  73.     <li class="parent" id="<?php the_ID(); ?>">
  74.     <a class="ajax-click" href="#"><?php the_title(); ?></a>
  75.     <div class="child">
  76.     <?php the_content() ?>
  77.     </div>
  78.     </li>
  79.  
  80.     <?php
  81.  
  82.     $result = 'success';
  83.  
  84.   endwhile;
  85.    ?></ul><?php
  86.  else:
  87.     $output = '<h2>No posts found</h2>';
  88.     $result = 'fail';
  89.   endif;
  90.  
  91.   $response = json_encode($output);
  92.   echo $response;
  93.  
  94.   die();
  95.  
  96. }
  97.  
  98. add_action('wp_ajax_filter_terms', 'ajax_filter_get_terms');
  99. add_action('wp_ajax_nopriv_filter_terms', 'ajax_filter_get_terms');
  100.  
  101. // Enqueue script
  102.  
  103. // Enqueue script
  104. function ajax_filter_posts_scripts() {
  105.   // Enqueue script
  106.   wp_register_script('afp_script', get_stylesheet_directory_uri() . '/inc/ajax-filter-posts.js', false, null, false);
  107.   wp_enqueue_script('afp_script');
  108.  
  109.   wp_localize_script( 'afp_script', 'afp_vars', array(
  110.         'afp_nonce' => wp_create_nonce( 'afp_nonce' ), // Create nonce which we later will use to verify AJAX request
  111.         'afp_ajax_url' => admin_url( 'admin-ajax.php' ),
  112.       )
  113.   );
  114.  
  115. }
  116. add_action('wp_enqueue_scripts', 'ajax_filter_posts_scripts', 100);
  117.  
  118.     function my_load_ajax_content () {
  119.           if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
  120.     die('Permission denied');
  121.    
  122.         $anchor = '<a href="';
  123.         $close_anchor = '" class="fancybox-vimeo">here</a>';
  124.         $post_id = $_POST[ 'post_id' ];
  125.  
  126.         $post = get_post( $post_id, OBJECT);
  127.        
  128.         $post_audio = the_field('audio_file', $post);
  129.         $post_video = '.$anchor'.the_field('vimeo_link_double', $post)'.$close_anchor.';
  130.         $response = apply_filters( 'the_content', $post->post_content, $post_audio, $post_video );
  131.  
  132.         echo $response;
  133.         die(1);
  134.     }
  135.  
  136. add_action('wp_ajax_load-content', 'my_load_ajax_content');
  137. add_action ( 'wp_ajax_nopriv_load-content', 'my_load_ajax_content' );
  138. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement