weixin_33713707 2018-03-27 16:28 采纳率: 0%
浏览 21

WordPress的ajax WP_Query

I'm trying to load post with ajax using a variable to get posts by year. I've found this great tutorial https://www.creare.co.uk/blog/simple-wp_query-ajax and I'm trying to modify it to my needs but I have 0 experience with Ajax and not so much with jquery...

this is my modified code but the year is not passed and I don't know how to do it, I appreciate any help

js

//If input is changed, load posts
$('#genre-filter input').live('change', function(){
    genre_get_posts(); //Load Posts
});

//Find Selected Genres
function getSelectedGenres()
{
    var genres = [];

    $("#genre-filter div").click(function() {
        var genres = $(this).attr('value');
        console.log(genres);
    });        

    return genres;
}

//Main ajax function
function genre_get_posts(paged)
{

    var ajax_url = ajax_genre_params.ajax_url;

    $.ajax({
        type: 'GET',
        url: ajax_url,
        data: {
            action: 'genre_filter',
            genres: getSelectedGenres,

        },
        beforeSend: function ()
        {
            //Show loader here
        },
        success: function(data)
        {
            //Hide loader here
            $('#genre-results').html(data);
        },
        error: function()
        {
            $("#genre-results").html('<p>There has been an error</p>');
        }
    });                
}

template

<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
    <?php
    if ( have_posts() ):
        while ( have_posts() ): the_post();
    get_template_part( 'content' );
    endwhile;
    endif;
    ?>
    <div class="entry-content">
        <div id="genre-filter">
            <div id="2018" value="2018" name="filter_genre[]">2018</div>
            <div id="2017" value="2017" name="filter_genre[]">2017</div>
      </div>
        <div id="genre-results"></div>
    </div>
</div>

functions.php

function ajax_genre_filter() {
    $query_data = $_GET;
    $genre_terms = ( $query_data[ 'genres' ] ) ? explode( ',', $query_data[ 'genres' ] ) : false;
    $book_args = array(
        'post_type' => 'book',
        'posts_per_page' => 2,
        'year' => $genre_terms,
    );
    $book_loop = new WP_Query( $book_args );

    if ( $book_loop->have_posts() ):
        while ( $book_loop->have_posts() ): $book_loop->the_post();
            get_template_part( 'content' );
        endwhile;
        echo $query_data;
    else :
        get_template_part( 'content-none' );
    endif;
    wp_reset_postdata();
    die();
}
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 c++字符串分割问题
      • ¥15 关于#sql#的问题:没有用命令关闭cdc,手动把系统表开启的cdc右键删除了
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题
      • ¥15 win11如何运行geoserver
      • ¥15 审稿意见请教-用ERNIE进行小样本分类的对比实验问题
      • ¥15 gem5搭建arm v8