簡體   English   中英

如何更改 WP_Query 中的帖子類別

[英]How to change category of post in WP_Query

單擊具有類別名稱的特定按鈕時,如何將類別名稱傳遞給新的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”,但我希望它是動態的。

在這里,我們將學習如何在 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);
      }
     });
});
    });

參數的實現

如果您在主題自定義編碼中使用,則將以下代碼放在主題的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' );

您的代碼應如下所示。

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

並且當您當時使用 jquery 時,您需要在其上傳遞類別 ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM