在WordPress上使用Ajax进行类别过滤

问题描述 投票:0回答:1

[当我选择类别时,帖子不会按类别/分类法进行筛选-所有帖子都只会显示在页面上。

我已经尝试调试,但是没有运气。任何帮助将不胜感激!

这也是我的项目文件的github链接:https://github.com/guyku/WordPressAjax_RyanMcGovern_Lesson

index.php:

<!DOCTYPE html>
<html <?php language_attributes(); ?>>

  <body>

    <div id="content">
     <?php include('content.php'); ?>
    </div>

  </body>
</html>

content.php:

<div class="js-filter">
    <?php

    $args = array(
        'post_type' => 'post',
        'posts_per_page' => -1
        );

        $query = new WP_Query($args);

        if($query->have_posts()) :
            while($query->have_posts()) : $query->the_post();
                the_title('<h2>', '</h2>');
                the_content('<p>', '</p>');
            endwhile;
            endif;
            wp_reset_postdata(); ?>
</div>

<div class="categories">
    <ul>
        <li class="js-filter-item"><a href="<?= home_url(); ?>">All</a></li>
<?php 
$cat_args = array(
    'exclude' => array(1),
    'option_all' => 'All'
);

$categories = get_categories($cat_args);

foreach($categories as $cat) : ?>
    <li class="js-filter-item"><a data-category="<?= $cat->term_id; ?>" href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
<?php endforeach; ?>
</ul>
</div>

scripts.js:

(function($){

    $(document).ready(function(){
        $(document).on('click', '.js-filter-item > a', function(e){
            e.preventDefault();

            var category = $(this).data('category');

            $.ajax({
                url:wp_ajax.ajax_url,
                data: { action: 'filter', category: category },
                type: 'post',
                success: function(result) {
                    $('.js-filter').html(result);
                },
                error: function(result) {
                    console.warn(result);
                }
            });
        });
    });

})(jQuery);

scripts.php:

<?php
/*
* Enqueue scripts.js if file scripts.js exists
*/
function load_scripts() {

    wp_enqueue_script('ajax', get_template_directory_uri() . 'scripts.js', array('jquery'), NULL, true);

    wp_localize_script('ajax' , 'wp_ajax',
        array('ajax_url' => admin_url('admin-ajax.php'))
        );

}

add_action( 'wp_enqueue_scripts', 'load_scripts');

?>

example.php:

<?php

add_action( 'wp_ajax_nopriv_filter', 'filter_ajax' );
add_action( 'wp_ajax_filter', 'filter_ajax' );

function filter_ajax() {


$category = $_POST['category'];

$args = array(
        'post_type' => 'post',
        'posts_per_page' => -1
        );

if(isset($category)) {
    $args['category__in'] = array($category);
}

        $query = new WP_Query($args);

        if($query->have_posts()) :
            while($query->have_posts()) : $query->the_post();
                the_title('<h2>', '</h2>');
                the_content('<p>', '</p>');
            endwhile;
        endif;
            wp_reset_postdata(); 


    die();
}
?>

functions.php:

<?php

require_once('scripts.php');
require_once('example.php');

?>
php jquery ajax wordpress categories
1个回答
0
投票

在您的content.php文件中

您必须添加类别名称$ args = array(

'post_type' => 'post',
'posts_per_page' => -1,
'category_name' => 'Your_Category_Name'
    );
© www.soinside.com 2019 - 2024. All rights reserved.