2017-07-28 111 views
0

我已經在我的WordPress主題中創建了自定義搜索表單。她看起來像:按標籤自定義AJAX搜索WP

<form role="search" class="form" method="get" action="<?php echo home_url('/');?>"> 
    <input type="search" id="keyword" class="inp-search" onclick="showNewTag()" oninput="searchTags()" onkeyup="fetch()" placeholder="search by tag" value="<?php echo get_search_query() ?>" name="s" title="Search" /> 

,我意識到通過文章標題自定義AJAX搜索。但我的任務是通過標籤進行搜索。我不知道我該怎麼做。我通過職位名稱搜索位於functions.php,看起來像:

add_action('wp_footer', 'ajax_fetch'); 
function ajax_fetch() { 
?> 
<script type="text/javascript"> 
    function fetch(){ 

    jQuery.ajax({ 
    url: '<?php echo admin_url('admin-ajax.php'); ?>', 
    type: 'post', 
    data: { action: 'data_fetch', keyword: jQuery('#keyword').val() }, 
    success: function(data) { 
     jQuery('#grid').html(data); 
    } 
}); 

} 
</script> 

<?php 
} 

add_action('wp_ajax_data_fetch' , 'data_fetch'); 
add_action('wp_ajax_nopriv_data_fetch','data_fetch'); 
function data_fetch(){ 

    $the_query = new WP_Query(array('posts_per_page' => -1, 's' => 
esc_attr($_POST['keyword']), 'post_type' => 'post')); 
if($the_query->have_posts()) : 
    while($the_query->have_posts()): $the_query->the_post(); 

     if(get_field('type') == "box") { 
      ?> 
      <div class="grid-item grid-item--<?php echo get_field('type');?> <?php echo get_field('position');?>" data-tooltitle="Hood Baroque" data-tooltip="Design | 3D modeling | Drawings"> 
       <a class="linkpost" href="<?php echo get_post_permalink()?>"><img src="<?php the_field('picture');?>" alt=""></a> 
       <a href="<?php echo get_post_permalink()?>" class="text"> 
        <img src="<?php the_field('svg_picture');?>" alt=""> 
       </a> 
      </div> 
    <?php 

     } else { 
    ?> 
    <div class="grid-item grid-item--<?php echo get_field('type');?> left" data-tooltitle="Hood Baroque" data-tooltip="Design | 3D modeling | Drawings"> 
     <a class="linkpost" href="<?php echo get_post_permalink()?>"><img src="<?php echo get_field('picture');?>" alt=""></a> 
      <a href="<?php echo get_post_permalink()?>" class="text"> 
       <img src="<?php echo get_field('svg_picture');?>" alt=""> 
      </a> 
    </div> 

    <?php 
     } 

    endwhile; 
    wp_reset_postdata(); 
endif; 

die(); 
} 

我如何才能實現通過標籤搜索?

+0

設置合適的*「標籤參數」*,如'WP_Query'文檔中所述 – charlietfl

回答

0

codex

顯示文章從幾個標籤

具有的「要麼」這些標籤

的帖子開始顯示:有「所有」這些標籤

$query = new WP_Query(array('tag' => 'bread,baking')); 

顯示文章:

$query = new WP_Query(array('tag' => 'bread+baking+recipe'));