简体   繁体   English

如何更改 WP_Query 中的帖子类别

[英]How to change category of post in WP_Query

How can I pass a category name to new WP_Query when I click specific button with category name?单击具有类别名称的特定按钮时,如何将类别名称传递给新的WP_Query

I've got this in my functions.php我在我的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();
}
?>

and this on page with my default loop posts这个页面上有我的默认循环帖子

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();
});

I want load a new query with new loop based on category choose when I click a button.我想在单击按钮时根据类别选择加载带有新循环的新查询。 Now I set category '2017' but I want it to be dynamic.现在我设置了类别“2017”,但我希望它是动态的。

Here we will learn how to use AJAX in WordPress.在这里,我们将学习如何在 WordPress 中使用 AJAX。 We will see how WordPress AJAX works as Beginner level.我们将看到 WordPress AJAX 如何在初学者级别工作。 In this, we will pass a variable from JavaScript and pass it to WordPress theme function file.在此,我们将从 JavaScript 传递一个变量并将其传递给 WordPress 主题函数文件。 After doing the necessary process, we will pass the resulting content back to the JavaScript.在完成必要的过程后,我们会将结果内容传递回 JavaScript。

We are assuming that you already know how to enqueue JavaScript, etc.我们假设您已经知道如何对 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);
      }
     });
});
    });

Implementation of Argument参数的实现

If you are using in theme custom coding then put the below code in theme's functions.php file如果您在主题自定义编码中使用,则将以下代码放在主题的functions.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' );

Your code should look like this.您的代码应如下所示。

$args=array(
    'posts_per_page' => 50, 
    'post_type' => 'my_custom_type'
    'cat' => $cat_id,
);
$wp_query = new WP_Query( $args );

and when you use jquery at that time you need to pass category id on that.并且当您当时使用 jquery 时,您需要在其上传递类别 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM