第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用復(fù)選框制作帖子類別過(guò)濾器?

如何使用復(fù)選框制作帖子類別過(guò)濾器?

PHP
慕工程0101907 2021-06-10 15:44:27
在我的博客存檔頁(yè)面上,我嘗試使用復(fù)選框創(chuàng)建類別過(guò)濾器,以便訪問(wèn)者可以按類別過(guò)濾帖子,而無(wú)需重新加載頁(yè)面。但是我遇到了如何隱藏未選中類別的項(xiàng)目的問(wèn)題。我寫了一段代碼,但真的不知道如何從那里走得更遠(yuǎn)。$('.category-filter input[type="checkbox"]').click(function() {  if ($(this).is(":checked")) {    var categoryName = $(this).attr("name");    if ($("article").hasClass('category' + categoryName)) {}    $(this).toggle;  } else if ($(this).is(":not(:checked)")) {  }});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><aside class="category-filter">  <label class="filter-category"><input type="checkbox" value="7" name="block" class=" js-filter-checkbox" id="7">Block</label>  <label class="filter-category"><input type="checkbox" value="16" name="classic" class=" js-filter-checkbox" id="16">Classic</label>  <label class="filter-category"><input type="checkbox" value="22" name="edge-case-2" class=" js-filter-checkbox" id="22">Edge Case</label></aside><main>  <article id="post-2131" class="post-2131 post type-post status-publish format-standard has-post-thumbnail sticky hentry category-geen-onderdeel-van-een-categorie"></article>  <article id="post-1241" class="post-1241 post type-post status-publish format-standard sticky hentry category-classic category-uncategorized tag-sticky-2 tag-template"></article>  <article id="post-1938" class="post-1938 post type-post status-publish format-standard hentry category-block"></article></main>我想隱藏沒(méi)有檢查任何類的文章。
查看完整描述

3 回答

?
神不在的星期二

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊

這需要使用 AJAX 來(lái)完成。創(chuàng)建這個(gè) AJAX 過(guò)濾器需要幾個(gè)步驟。


首先,如果您希望一次只能單擊復(fù)選框,則需要確保您都具有相同的名稱屬性值。因此,您應(yīng)該同時(shí)調(diào)用它們,而不是 name="block" 或 name="classic",例如,name="category-checkbox"。


在您的functions.php 文件中,您需要將以下內(nèi)容入隊(duì):


function scripts() {

     wp_localize_script ('scripts', 'js_variables', array ('js_home_url' => 

     home_url(), 'ajaxURL' => admin_url( 'admin-ajax.php' ), ''));

}

add_action( 'wp_enqueue_scripts', 'scripts' );

在您的 javascript 文件中,您需要從復(fù)選框中收集數(shù)據(jù)。您可以使用以下方法執(zhí)行此操作:


$('.filter-category').change(function() {

  var catId = ($(this).val());

  aJaxFilter(catId);

});


function aJaxFilter(formData) {

  $.ajax({

    url: js_variables.ajaxURL,

    type:'GET',

    data: {

      'formData' : formData,

      'action' : 'listFilteredProducts'

    },

    success:function(data){

      $('main').html(data);

    }

  });

}

然后,您需要?jiǎng)?chuàng)建另一個(gè)文件,我將其命名為 ajax-function.php,并將其放在我的代碼段文件夾中。在那里,輸入以下代碼:


add_action('wp_ajax_listFilteredProducts', 'listFilteredProducts');

add_action('wp_ajax_nopriv_listFilteredProducts', 'listFilteredProducts');


function listFilteredProducts($wp_query) {

  if(isset($_GET['formData'])) {

    $cat_id = $_GET['formData'];

  }

  $args = array(

  'post_type' => 'post',

  'posts_per_page' => -1,

  'cat' => $cat_id,

  );

  $multiple = get_posts($args);

  foreach($multiple as $single) {

  $ID = $single->ID;

   // Put the code for how you want your article cards to look here.

  }

  wp_die();

}


查看完整回答
反對(duì) 回復(fù) 2021-06-19
?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

我可以想到兩個(gè)選項(xiàng)來(lái)解決這個(gè)問(wèn)題。第一種是在過(guò)濾器更改時(shí)使用 AJAX 重新加載結(jié)果列表。

第二個(gè)選項(xiàng)(看起來(lái)像您正在嘗試的那個(gè))是將數(shù)據(jù)屬性中的每個(gè)帖子的類別添加到元素(逗號(hào)分隔),并且當(dāng)過(guò)濾器更改時(shí),循環(huán)遍歷每個(gè)帖子元素并檢查是否category 存在,然后隱藏或顯示每個(gè)帖子元素。

這兩個(gè)選項(xiàng)之間的選擇取決于您顯示的項(xiàng)目數(shù)量以及您是否使用分頁(yè)。第二個(gè)選項(xiàng)是最簡(jiǎn)單的選項(xiàng),但是當(dāng)您使用分頁(yè)時(shí),它會(huì)給您帶來(lái)麻煩,因?yàn)椴⒎撬刑佣紩?huì)一次加載。


查看完整回答
反對(duì) 回復(fù) 2021-06-19
  • 3 回答
  • 0 關(guān)注
  • 133 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)