ajax category filter

时间:2016-01-07 作者:rudtek

我知道这对你们来说是一个很大的要求,但我似乎无法理解。

我找到此页面:http://www.bobz.co/ajax-filter-posts-tag/#comment-28112

它显示了如何为post标记创建动态过滤器。

我想把它改成帖子类别,但我似乎无法让它工作。

我将此代码放在函数中。php

function ajax_filter_posts_scripts() {
  // Enqueue script
  wp_register_script(\'afp_script\', get_stylesheet_directory_uri() . \'/js/ajax-filter-posts.js\', false, null, false);
  wp_enqueue_script(\'afp_script\');

  wp_localize_script( \'afp_script\', \'afp_vars\', array(
        \'afp_nonce\' => wp_create_nonce( \'afp_nonce\' ), // Create nonce which we later will use to verify AJAX request
        \'afp_ajax_url\' => admin_url( \'admin-ajax.php\' ),
      )
  );
}
add_action(\'wp_enqueue_scripts\', \'ajax_filter_posts_scripts\', 100);

// Script for getting posts
function ajax_filter_get_posts( $taxonomy ) {

  // Verify nonce
  if( !isset( $_POST[\'afp_nonce\'] ) || !wp_verify_nonce( $_POST[\'afp_nonce\'], \'afp_nonce\' ) )
    die(\'Permission denied\');

  $taxonomy = $_POST[\'taxonomy\'];

  // WP Query
  $args = array(
    \'category_name\' => $taxonomy,
    \'post_type\' => \'post\',
    \'posts_per_page\' => 10,
  );

  // If taxonomy is not set, remove key from array and get all posts
  if( !$taxonomy ) {
    unset( $args[\'category_name\'] );
  }

  $query = new WP_Query( $args );

  if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>

  <?php endwhile; ?>
  <?php else: ?>
    <h2>No posts found</h2>
  <?php endif;

  die();
}

add_action(\'wp_ajax_filter_posts\', \'ajax_filter_get_posts\');
add_action(\'wp_ajax_nopriv_filter_posts\', \'ajax_filter_get_posts\');
然后在我的实际页面模板中,我输入以下代码:

//in my template file
<?php
$args = array(
    \'post_type\' => \'post\',
    \'posts_per_page\' => 10,
);

$query = new WP_Query( $args );

function tags_filter() {
    $tax = \'category\';
    $terms = get_terms( $tax );
    $count = count( $terms );

    if ( $count > 0 ): ?>
        <div class="post-tags">
        <?php
        foreach ( $terms as $term ) {
            $term_link = get_term_link( $term, $tax );
            echo \'<a href="\' . $term_link . \'" class="tax-filter" title="\' . $term->slug . \'">\' . $term->name . \'</a> \';
        } ?>
        </div>
    <?php endif;
}
当我加载页面模板时,网站会加载我的内容并显示类别筛选按钮,但当我单击任何按钮时,会返回“未找到帖子”。

这让我相信我的函数文件有问题,但我无法解决。

有人能看到我做错了什么吗?

2 个回复
最合适的回答,由SO网友:Daniel Griffiths 整理而成

不确定你是否解决了这个问题,但我正在寻找一种方法,将其嵌入到页面中,并按类别过滤帖子。

我得到了这个工作,所以它显示所有类别和相关的职位。把它放进去functions.php

function ajax_filter_posts_scripts() {
  // Enqueue script
  wp_register_script(\'afp_script\', get_template_directory_uri() . \'/js/ajax-filter-posts.js\', false, null, false);
  wp_enqueue_script(\'afp_script\');

  wp_localize_script( \'afp_script\', \'afp_vars\', array(
        \'afp_nonce\' => wp_create_nonce( \'afp_nonce\' ), // Create nonce which we later will use to verify AJAX request
        \'afp_ajax_url\' => admin_url( \'admin-ajax.php\' ),
      )
  );
}
add_action(\'wp_enqueue_scripts\', \'ajax_filter_posts_scripts\', 100);

// Script for getting posts
function ajax_filter_get_posts( $taxonomy ) {

  // Verify nonce
  if( !isset( $_POST[\'afp_nonce\'] ) || !wp_verify_nonce( $_POST[\'afp_nonce\'], \'afp_nonce\' ) )
    die(\'Permission denied\');

  $taxonomy = $_POST[\'taxonomy\'];

  // WP Query
  $args = array(
    \'category_name\' => $taxonomy,
    \'post_type\' => \'post\',
    \'posts_per_page\' => 10,
  );
  echo $taxonomy;
  // If taxonomy is not set, remove key from array and get all posts
  if( !$taxonomy ) {
    unset( $args[\'tag\'] );
  }

  $query = new WP_Query( $args );

  if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>

  <?php endwhile; ?>
  <?php else: ?>
    <h2>No posts found</h2>
  <?php endif;

  die();
}

add_action(\'wp_ajax_filter_posts\', \'ajax_filter_get_posts\');
add_action(\'wp_ajax_nopriv_filter_posts\', \'ajax_filter_get_posts\');
然后,将其添加到页面模板中:

<?php $args = array(
            \'post_type\' => \'post\',
            \'posts_per_page\' => 10,
        );

        $query = new WP_Query( $args );

        $tax = \'category\';
        $terms = get_terms( $tax );
        $count = count( $terms );

        if ( $count > 0 ): ?>
            <div class="post-tags">
            <?php
            foreach ( $terms as $term ) {
                $term_link = get_term_link( $term, $tax );
                echo \'<a href="\' . $term_link . \'" class="tax-filter" title="\' . $term->slug . \'">\' . $term->name . \'</a> \';
            } ?>
            </div>
        <?php endif;
        if ( $query->have_posts() ): ?>
        <div class="tagged-posts">
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>

            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>

            <?php endwhile; ?>
        </div>

        <?php else: ?>
            <div class="tagged-posts">
                <h2>No posts found</h2>
            </div>
        <?php endif; ?>
希望这有助于解决您的问题!

SO网友:Droid Sheep

在tags\\u过滤器中更改$tax = \'post_tag\';到这个$tax = \'category\';

然后在WP查询中更改\'tag\' => $taxonomy,到这个\'category\' => $taxonomy,

对我来说很好。。。

相关推荐

在wp_Dropdown_Categories数组中搜索后WordPress默认类别和自定义分类选择的属性不起作用

表单中有三个字段,第一个是默认WordPress类别和两个自定义分类法。当我尝试搜索单个字段时“;“选定属性”;正确,但在“类别”、“选择分类法”“选定值”上取消选择,并且不显示选定属性。代码如下: <form method="get" action="<?php echo get_site_url(); ?>" class=""> <!--categorie