使用AJAX获取当前分类帖子

时间:2017-01-18 作者:amirali bk

这是我的页面ajax。php文件


define(\'WP_USE_THEMES\', false);
require_once(\'../../../wp-load.php\');

   $terms = get_terms( \'bk-category\' );
   foreach ( $terms as $term ) {
 }

$bkcat = $term->term_id;
     $args = array(
    \'posts_per_page\'   => -1,
    \'category\'         => $bkcat,
    \'order\'            => \'ASC\',
    \'post_type\'        => \'bk-post\',

    );
     $the_query = new WP_Query($args);
     if($the_query->have_posts()) {
            ?>

                     $i = 1;
                     while($the_query->have_posts()) : $the_query->the_post();
                     $a = $i++;

                      endwhile;
                     }



这是我的页面表。php文件


global $post;
  $terms = get_terms( \'bk-category\' );
  $select = "n";
  $select.= "Select categoryn";

  foreach($terms as $term){
    if($term->count > 0){
        $select.= "slug."\'>".$term->name."";
    }
  }

  $select.= "";

  echo $select;




$(document).ready(function() {
   var dropdown = document.getElementById("cat");
    function onCatChange() {
        if ( dropdown.options[dropdown.selectedIndex].value != -1 ) {
    $(\'.results\').html("/images/loading.gif\' />");
    $.ajax({
        type: \'POST\',
        async: true,
        dataType   : "html",
        url: "/page-ajax.php",
        success: function (data){
            $(\'.results\').html(data);
        }
    });
        }
    }
    dropdown.onchange = onCatChange;

});


在本例中,所有显示或所有类别显示,但是:我只想显示单击的每个类别的内容

1 个回复
SO网友:Pratik bhatt

//$bkcat = $term->term_id; try to call this via post from ajax where you will pass id of category
 $args = array(
\'posts_per_page\'   => -1,
\'category\'         => $bkcat,
\'order\'            => \'ASC\',
\'post_type\'        => \'bk-post\',
\'category_name\'    => $bkcat,

);
 $the_query = new WP_Query($args);
 if($the_query->have_posts()) {
        ?>

                 $i = 1;
                 while($the_query->have_posts()) : $the_query->the_post();
                 $a = $i++;

                  endwhile;
                 }
如果您有疑问,请告诉我,我将向您解释整个代码。

相关推荐

无法使用AJAX访问数据库中的数据

我正试图在wordpress中首次调用AJAX。我遵循了一些教程,到目前为止已经达到了这一点。但当我试图安慰的时候。将从数据库中获取的数据记录在AJAX调用中,我发现以下错误:未捕获引用错误:未定义数据代码:功能。phpfunction my_ajax_handler(){ global $wpdb; $name = $wpdb->get_results(\"SELECT * FROM username\"); echo $name;