Advertisement
Mimeobrad

Query and script

Oct 29th, 2021
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.40 KB | None | 0 0
  1. <?php
  2. $categories = get_terms('faq_cat'); ?>
  3. <ul class="cat-list">
  4.   <li><a class="cat-list_item active" href="#!" data-slug="">All FAQS</a></li>
  5.   <?php foreach($categories as $category) : ?>
  6.     <li>
  7.       <a class="cat-list_item" href="#!" data-slug="<?= $category->slug; ?>">
  8.         <?= $category->name; ?>
  9.       </a>
  10.     </li>
  11.   <?php endforeach; ?>
  12. </ul>
  13.  
  14. <?php
  15. $termIds = array('1821','1820');
  16. $faqs = new WP_Query([
  17.    'post_type' => 'faqs',
  18.    'posts_per_page' => -1,
  19.    'tax_query' => [
  20.       [
  21.          'taxonomy' => 'faq_cat',
  22.          'field'    => 'term_id',
  23.          'terms'    =>  $termIds,
  24.          'operator' => 'IN'
  25.       ],
  26.    ]
  27. ]);?>
  28.  
  29. <?php if($faqs->have_posts()): ?>
  30.   <ul class="faq-tiles">
  31.     <?php
  32.       while($faqs->have_posts()) : $faqs->the_post();
  33.         include('templates/faq-single.php');
  34.       endwhile;
  35.     ?>
  36.   </ul>
  37.   <?php wp_reset_postdata(); ?>
  38. <?php endif; ?>
  39.  
  40. <script>
  41.   jQuery(document).ready(function($) {
  42.     $('.cat-list_item').on('click', function() {
  43.   $('.cat-list_item').removeClass('active');
  44.   $(this).addClass('active');
  45.  
  46.   $.ajax({
  47.     type: 'POST',
  48.     url: '/wp-admin/admin-ajax.php',
  49.     dataType: 'html',
  50.     data: {
  51.       action: 'filter_faqs',
  52.       category: $(this).data('slug'),
  53.     },
  54.     success: function(res) {
  55.       console.log('success');
  56.       $('.faq-tiles').html(res);
  57.     }
  58.   })
  59. });
  60. });
  61. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement