Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Script for getting posts
- function ajax_filter_get_posts( $taxonomy ) {
- // Verify nonce
- if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
- die('Permission denied');
- $taxonomy = $_POST['taxonomy'];
- // WP Query
- $args = array(
- 'glossary_category' => $taxonomy,
- 'post_type' => 'glossary_item',
- 'posts_per_page' => -1,
- );
- // If taxonomy is not set, remove key from array and get all posts
- if( !$taxonomy ) {
- unset( $args['cat'] );
- }
- $query = new WP_Query( $args );
- if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
- echo '<h2>'.the_title().'</h2>';
- $output = the_content();
- $result = 'success';
- endwhile; else:
- $output = '<h2>No posts found</h2>';
- $result = 'fail';
- endif;
- $response = json_encode($output);
- echo $response;
- die();
- }
- add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
- add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
- // Script for getting terms
- function ajax_filter_get_terms( $taxonomy ) {
- // Verify nonce
- if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
- die('Permission denied');
- $taxonomy = $_POST['taxonomy'];
- // WP Query
- $args = array(
- 'encyclopedia_category' => $taxonomy,
- 'post_type' => 'encyclopedia_item',
- 'posts_per_page' => -1,
- );
- // If taxonomy is not set, remove key from array and get all posts
- if( !$taxonomy ) {
- unset( $args['encyclopedia_category'] );
- }
- $query = new WP_Query( $args );
- ?> <ul><?php
- if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
- ?>
- <li class="parent" id="<?php the_ID(); ?>">
- <a class="ajax-click" href="#"><?php the_title(); ?></a>
- <div class="child">
- <?php the_content() ?>
- </div>
- </li>
- <?php
- $result = 'success';
- endwhile;
- ?></ul><?php
- else:
- $output = '<h2>No posts found</h2>';
- $result = 'fail';
- endif;
- $response = json_encode($output);
- echo $response;
- die();
- }
- add_action('wp_ajax_filter_terms', 'ajax_filter_get_terms');
- add_action('wp_ajax_nopriv_filter_terms', 'ajax_filter_get_terms');
- // Enqueue script
- // Enqueue script
- function ajax_filter_posts_scripts() {
- // Enqueue script
- wp_register_script('afp_script', get_stylesheet_directory_uri() . '/inc/ajax-filter-posts.js', false, null, false);
- wp_enqueue_script('afp_script');
- wp_localize_script( 'afp_script', 'afp_vars', array(
- 'afp_nonce' => wp_create_nonce( 'afp_nonce' ), // Create nonce which we later will use to verify AJAX request
- 'afp_ajax_url' => admin_url( 'admin-ajax.php' ),
- )
- );
- }
- add_action('wp_enqueue_scripts', 'ajax_filter_posts_scripts', 100);
- function my_load_ajax_content () {
- if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
- die('Permission denied');
- $anchor = '<a href="';
- $close_anchor = '" class="fancybox-vimeo">here</a>';
- $post_id = $_POST[ 'post_id' ];
- $post = get_post( $post_id, OBJECT);
- $post_audio = the_field('audio_file', $post);
- $post_video = '.$anchor'.the_field('vimeo_link_double', $post)'.$close_anchor.';
- $response = apply_filters( 'the_content', $post->post_content, $post_audio, $post_video );
- echo $response;
- die(1);
- }
- add_action('wp_ajax_load-content', 'my_load_ajax_content');
- add_action ( 'wp_ajax_nopriv_load-content', 'my_load_ajax_content' );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement