3

好吧,我一直在到处寻找几个小时,似乎有很多方法可以做到这一点,因为我以前从未使用过 Ajax 并且对 havascript 知之甚少,这对我来说太难了。

我的首页(索引)或 wordpress 上有循环,我想要一个过滤器,一个具有不同类别的下拉菜单,当点击它时,在同一屏幕上显示的唯一帖子是来自该类别的帖子。我需要使用 ajax 刷新循环,因此在您使用过滤器时整个页面仍然完好无损。

这是我的索引文件中的内容:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">google.load("jquery", "1.2.6")</script>
<script type="text/javascript">
$(function(){
     $('#main_cat').change(function(){
         var $mainCat=$('#main_cat').val();
          $("#sub_cat").empty();
              // call ajax
                  $.ajax({

              url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",       
              type:'POST',
              data:'action=my_special_ajax_call&main_catid=' + $mainCat,
              success:function(results)
              {
            //  alert(results);
              $('#sub_cat *').fadeOut(500);
              $('#sub_cat + p').fadeOut(500);
              $("#sub_cat").append(results);
                  $('#sub_cat').load('http://localhost:8888/public_html/wp-content/themes/twentyten-child/templateloop.php');
              $('#sub_cat + p').fadeIn(1);
                  }
                      });

            }
    );
});

带有类别的下拉菜单如下所示:

<?php
wp_dropdown_categories('show_count=0&selected=-1&hierarchical=1&depth=1&hide_empty=0&exclude=1&show_option_none=Main Categories&name=main_cat');
?>

因此,下拉菜单有效,它应该通过仅过滤一个类别的查询(从 wp_dropdown_categories 中获取)加载 wp 模板文件。如果我在 templateloop.php 文件中有一个虚拟文本,加载工作正常,但是当我有 wp 查询时,什么也没有发生。#sub_cat div,这是循环所在的位置,应该由模板文件切换,所有帖子列表都消失了,我只剩下页面的上半部分(直到 #sub_cat div 曾经是)。

有这么多的试验和错误,我尝试在模板文件、索引文件、函数中使用查询调用,我似乎从来没有得到任何结果。

在我的functions.php文件上,我得到了这个:

function implement_ajax() {
if(isset($_POST['main_catid']))
            {
              echo '<?php $paged = (get_query_var("paged")) ? get_query_var("paged") : 1; query_posts("cat='.$_GET['maincatid'].'&paged=$paged"); ?>';

            die();
            } // end if
}
add_action('wp_ajax_my_special_ajax_call', 'implement_ajax');
add_action('wp_ajax_nopriv_my_special_ajax_call', 'implement_ajax');//for users that are not logged in.

我在这之前使用的查询行在索引文件中是:

       <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 query_posts("cat=1,2,3,4,5&paged=$paged"); ?>

我试过使用 wp_query 但它无处可去,我真的需要指导。任何帮助表示赞赏。谢谢你。

4

1 回答 1

0

That is more complicated than it needs to be. You shouldn't need additional queries or AJAX at all. If you theme is using the post_class() function as it should be, your posts all have classes associated with your categories. These classes are the category name prepended with 'category-'-- 'category-uncategorized', for example. All you really need to do is show and hide posts based on those classes.

I haven't written anything specifically for your circumstance but I have done this with some very large search results-- sometimes 400 or more per page-- and it works quite well. Here is the idea: Use jQuery to watch your select menu. You want change. When the select changes, parse the information to work out the category (I don't know off hand what information wp_dropdown_categories() includes in its markup.), show() the selected category and hide() everything else.

于 2012-10-13T15:01:07.600 回答