Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NO changes in js filter
- // functions.php
- function filter_projects() {
- $catSlug = $_POST['category'];
- $args = array(
- 'post_type' => 'project',
- 'tax_query' => array(
- array(
- 'taxonomy' => 'project-type',
- 'field' => 'slug',
- 'terms' => $catSlug,
- ),
- ),
- );
- $ajaxposts = new WP_Query( $args );
- $response = '';
- if($ajaxposts->have_posts()) {
- while($ajaxposts->have_posts()) : $ajaxposts->the_post();
- $response .= get_template_part('template-parts/project');
- endwhile;
- } else {
- $response = 'No projects';
- }
- echo $response;
- exit;
- }
- add_action('wp_ajax_filter_projects', 'filter_projects');
- add_action('wp_ajax_nopriv_filter_projects', 'filter_projects');
- // page-portfolio.php -- main content
- <?php $categories = get_terms( [
- 'taxonomy' => 'project-type',
- 'hide_empty' => false,
- ] ); ?>
- <div class="portfolio__filter">
- <li><a class="cat-list_item current-menu-item" href="/portfolio" data-slug="">All projects</a></li>
- <?php foreach($categories as $category) : ?>
- <li>
- <a class="cat-list_item" href="#!" data-slug="<?= $category->slug; ?>">
- <?= $category->name; ?>
- </a>
- </li>
- <?php endforeach; ?>
- </div>
- <?php
- $projects = new WP_Query([
- 'post_type' => 'project',
- 'posts_per_page' => -1,
- 'order_by' => 'date',
- 'order' => 'desc',
- ]);
- ?>
- <?php if($projects->have_posts()): ?>
- <div class="portfolio__content">
- <?php
- while($projects->have_posts()) : $projects->the_post();
- get_template_part('template-parts/project');
- endwhile;
- ?>
- </div>
- <?php wp_reset_postdata(); ?>
- <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement