Wordpress ajax WP_Query

时间:2018-03-27 16:28:56

标签: php ajax wordpress variables

我尝试使用变量加载ajax以按年份获得帖子。我找到了这个很棒的教程https://www.creare.co.uk/blog/simple-wp_query-ajax,我试图根据自己的需要对其进行修改,但我对Ajax有0次经验,而不是jquery ...

这是我的修改后的代码,但是这一年没有通过,我不知道该怎么做,我感谢任何帮助

js

#!/usr/bin/python3

import pdb
import openpyxl
from openpyxl.utils import column_index_from_string

wb1 = openpyxl.load_workbook('.../Extraction.xlsx')
wb2 = openpyxl.load_workbook('.../Template.xlsx')

ws1 = wb1.active
first_row1 = list(ws1.rows)[0]             #to select the first row (header)
for cell in first_row1:
    if cell.value == "email":
        x = cell.column                    #to get the column
        y = column_index_from_string(x)    #to get the column's index

for i in range(2, 469):
    cell_range1 = ws1.cell(i, y)           #the wrong part

ws2 = wb2.active
first_row2 = list(ws2.rows)[0]
for cell in first_row2:
    if cell.value == "emailAddress":
        w = cell.column
        z = column_index_from_string(w)

for o in range(2, 469):
    cell_range2 = ws2.cell(o, z)
    cell_range2.value = cell_range1.value

path = '.../Test.xlsx'
wb2.save(path)

模板

//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>');
        }
    });                
}

的functions.php

<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>

1 个答案:

答案 0 :(得分:0)

您的第一个问题是getSelectedGenres中的变量范围。由于事件处理程序中的第二个var,我会想象你总是得到一个空数组。试试下面的修改版本。

function getSelectedGenres()
{
  var genres = [];

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

  return genres;
}

此外,看起来ajax_genre_filter根据地图genres从类型查询字符串'year' => $genre_terms中读取年份,因此请尝试在函数getSelectedGenres中包含所选年份。