如何在WP_Query中更改帖子的类别

时间:2018-03-16 09:01:53

标签: php wordpress

当我点击带有类别名称的特定按钮时,如何将类别名称传递给新WP_Query

我的functions.php

中有这个
<?php
add_action('wp_ajax_my_action', 'data_fetch');
add_action('wp_ajax_nopriv_my_action', 'data_fetch');
function data_fetch(){
    $the_query = new WP_Query(array('post_type'=>'wydarzenie','posts_per_page'=>2, 'category_name'=>'2017'));
    if($the_query->have_posts()):
        while($the_query->have_posts()): $the_query->the_post(); ?>
              <h2><?php the_title(); ?></h2>
              <p><?php the_content(); ?></p>
        <?php endwhile;
        wp_reset_postdata();
    endif;
    die();
}
?>

并在页面上显示我的默认循环帖子

function fetch(){
    $.post('/PRACA/FundacjaWP/wp-admin/admin-ajax.php', {'action':'my_action'}, function(response){
        $("#pick-event").html(response);
    });
}

$(".show-specific-events").on("click", function(e){
    e.preventDefault();
    var category = $(this).text();
    fetch();
});

我希望在单击按钮时根据类别选择加载带有新循环的新查询。现在我设置了类别'2017',但我希望它是动态的。

2 个答案:

答案 0 :(得分:0)

您的代码应如下所示。

sysadmin@clickhouse-node1:/opt/zookeeper-3.4.11/bin$ ./zoo_clean.sh -n 10
/opt/zookeeper-3.4.11/data
/opt/zookeeper-3.4.11/logs
/usr/lib/jvm/java-8-oracle/bin/java -Dzookeeper.log.dir=. -Dzookeeper.root.logger=INFO,CONSOLE -cp /opt/zookeeper-3.4.11/bin/../build/classes:/opt/zookeeper-3.4.11/bin/../build/lib/*.jar:/opt/zookeeper-3.4.11/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/zookeeper-3.4.11/bin/../lib/slf4j-api-1.6.1.jar:/opt/zookeeper-3.4.11/bin/../lib/netty-3.10.5.Final.jar:/opt/zookeeper-3.4.11/bin/../lib/log4j-1.2.16.jar:/opt/zookeeper-3.4.11/bin/../lib/jline-0.9.94.jar:/opt/zookeeper-3.4.11/bin/../lib/audience-annotations-0.5.0.jar:/opt/zookeeper-3.4.11/bin/../zookeeper-3.4.11.jar:/opt/zookeeper-3.4.11/bin/../src/java/lib/*.jar:/opt/zookeeper-3.4.11/bin/../conf: org.apache.zookeeper.server.PurgeTxnLog /opt/zookeeper-3.4.11/logs /opt/zookeeper-3.4.11/data -n 10

当你在那时使用jquery时,你需要传递类别id。

答案 1 :(得分:0)

在这里,我们将学习如何在WordPress中使用AJAX。我们将看到WordPress AJAX如何作为初级水平。在这里,我们将从Javascript传递一个变量并将其传递给WordPress主题函数文件。完成必要的过程后,我们会将生成的内容传递回Javascript。

我们假设你已经知道如何排队javascript等等。

Javascript:

 jQuery(document).ready(function($) {        
     $(".show-specific-events").on("click", function(e){
    e.preventDefault();
    var category = $(this).text();

     // This does the ajax request
     $.ajax({
      url: codecanal_ajax_object.ajax_url,
      data: {
       'action':'codecanal_ajax_request',
       'category_name' : category
      },
      success:function(data) {
      // The OutPut after successfull receiveing content
      console.log(data);
      },
      error: function(errorThrown){
      console.log(errorThrown);
      }
     });
});
    });

参数的实施

如果您在主题自定义编码中使用,请将以下代码放在主题的function.php文件中

 function codecanal_ajax_request() {

     // The $_REQUEST contains all the data sent via ajax
     if ( isset($_REQUEST) ) {

     // You can check what data is received in the function by debugging it
     // print_r($_REQUEST);

     $category_name = $_REQUEST['category_name'];

$the_query = new WP_Query(array('post_type'=>'wydarzenie','posts_per_page'=>2, 'category_name'=> $category_name));
    if($the_query->have_posts()):
        while($the_query->have_posts()): $the_query->the_post(); ?>
              <h2><?php the_title(); ?></h2>
              <p><?php the_content(); ?></p>
        <?php endwhile;
        wp_reset_postdata();
    endif;
    die();

    }

    // To return to the front page, always finish after echoing the desired content.
    die();
    }
    add_action( 'wp_ajax_codecanal_ajax_request', 'codecanal_ajax_request' );

    // For allowing non-logged in users to use AJAX function
    // add_action( 'wp_ajax_nopriv_codecanal_ajax_request', 'codecanal_ajax_request' );

    /* We can define the AJAX url with using wp_localize_script */
    function codecanal_ajax_enqueue() {
     wp_localize_script( 'ajax-script', 'codecanal_ajax_object',
     array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    }
    add_action( 'wp_enqueue_scripts', 'codecanal_ajax_enqueue' );