Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FAQ Filter in archive-faqs.php
- <div class="container">
- <div class="row">
- <div class="col-md-3">
- <div class="bordered shadow-sm rounded">
- <?php $categories = get_terms('faq_cat');?>
- <ul class="cat-list">
- <li><a class="cat-list_item active" href="#!" data-slug="">All projects</a></li>
- <?php foreach($categories as $category) : ?>
- <li>
- <a class="cat-list_item" href="#!" data-slug="<?= $category->slug; ?>" data-type="faqs">
- <?= $category->name; ?>
- </a>
- </li>
- <?php endforeach; ?>
- </ul>
- </div>
- </div>
- <div class="col-md-9">
- <?php
- $projects = new WP_Query([
- 'post_type' => 'faqs',
- 'posts_per_page' => -1,
- 'orderby' => 'menu_order',
- 'order' => 'desc',
- ]);
- ?>
- <?php if($projects->have_posts()): ?>
- <h2 class="underline"><?= $category->name; ?></h2>
- <ul class="unstyled faq-cards">
- <?php
- while($projects->have_posts()) : $projects->the_post();
- ?>
- <li>
- <a class="question" data-toggle="collapse" href="#collapse-<?php the_ID(); ?>" role="button" aria-expanded="false" aria-controls="collapseExample">
- <?php the_title(); ?>
- </a>
- <div class="answer collapse" id="collapse-<?php the_ID(); ?>">
- <div class="card card-body">
- <p> <?php the_content(); ?></p>
- </div>
- </div>
- </li>
- <?php
- endwhile;
- ?>
- </ul>
- <?php wp_reset_postdata(); ?>
- <?php endif; ?>
- </div></div>
- <script>
- jQuery(function($){
- $('.cat-list_item').on('click', function() {
- $('.cat-list_item').removeClass('active');
- $(this).addClass('active');
- $.ajax({
- type: 'POST',
- url: '/wp-admin/admin-ajax.php',
- dataType: 'html',
- data: {
- action: 'filter_projects',
- category: $(this).data('slug'),
- type: $(this).data('type'),
- },
- success: function(res) {
- $('.faq-cards').html(res);
- }
- })
- });
- });
- </script>
- // FAQ Filter in functions
- function filter_projects() {
- $postType = $_POST['type'];
- $catSlug = $_POST['category'];
- $ajaxposts = new WP_Query([
- 'post_type' => $postType,
- 'posts_per_page' => -1,
- 'category_name' => $catSlug,
- 'orderby' => 'menu_order',
- 'order' => 'desc',
- ]);
- $response = '';
- // print_r($ajaxposts);
- if($ajaxposts->have_posts()) {
- while($ajaxposts->have_posts()) : $ajaxposts->the_post();
- $response = 'result';
- endwhile;
- } else {
- $response = 'empty';
- }
- echo $response;
- exit;
- }
- add_action('wp_ajax_filter_projects', 'filter_projects');
- add_action('wp_ajax_nopriv_filter_projects', 'filter_projects');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement