2015-09-28 76 views
0

我已經添加在WordPress的搜索高級搜索..請檢查下面的圖片.. enter image description here高級搜索未在Wordpress中顯示正確的結果?

下面是我的搜索代碼在WordPress:

global $wp_query; 
    $args = array (
    's' => $s, 
    'cat' => $category, 
    'year' => $year, 
    'monthnum' => $monthnum 
); 


$query = new WP_Query($args) ; 

<?php if ($query->have_posts()) : ?> 
    <?php while (have_posts()) : the_post(); ?> 
    <article> 
    <h2><?php the_title(); ?></h2> 
     <div class="entry"> 
     <?php the_content(__('Read More &raquo;', 'tie')); ?> 
     </div>   
    </article> 
    <?php endwhile; ?> 
    <?php endif; ?> 

從上面的代碼我得到正確的結果......但是當我改變類別的結果保持不變...?

,如果我得到的搜索結果爲1,2,3,4,5,6,7,8,9和變化對I類需要1,5,6,8,9

什麼是錯的用我的代碼.. ??

+0

檢查args數組'$ category'爲空或不是。 – Noman

回答

1

首先檢查你的$category爲空或不是。如果它沒有顯示與類別有關的信息。 也確保你$categoryinteger

看到什麼WP_QUERY使用類參數。

貓(INT) - 使用類別ID。
category_name(string) - 使用 category slug。
category__and(array) - 使用類別ID。
category__in(array) - 使用類別ID。
category__not_in(array) - 使用類別ID。

將您的循環更改爲下面的代碼。

對於使用分頁,您可能需要使用必需的參數來改變你的$ args數組。

//爲頁面的數量。

posts_per_pagepaged用於分頁。

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$per_page = 8; // records per page. 

$args = array (
    's' => $s, 
    'cat' => $category, 
    'year' => $year, 
    'monthnum' => $monthnum , 
    'posts_per_page' => $per_page, 
    'post_status' => 'publish', 
    'paged' => $paged, 
); 



// the query 
$query = new WP_Query($args); ?> 

<?php if ($query->have_posts()) : ?> 

    <!-- pagination here --> 

    <!-- the loop --> 
    <?php while ($query->have_posts()) : $query->the_post(); ?> 
     <h2><?php the_title(); ?></h2> 
    <?php endwhile; ?> 
    <!-- end of the loop --> 

    <!-- pagination here --> 

    <?php if (function_exists("pagination_custom")) { 
      pagination_custom($query->max_num_pages); 
    } 
    ?> 

    <?php wp_reset_postdata(); ?> 

<?php else : ?> 
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
<?php endif; ?> 

更新:對於分頁

// Pagination code 
function pagination_custom($pages = '', $range = 4) 
{ 
    $showitems = ($range * 2) + 1; 

    global $paged; 
    if (empty($paged)) $paged = 1; 

    if ($pages == '') { 
     global $wp_query; 
     $pages = $wp_query->max_num_pages; 
     if (!$pages) { 
      $pages = 1; 
     } 
    } 

    if (1 != $pages) { 
     echo "<div class=\"pagination\"><ul>"; 
     if ($paged > 2 && $paged > $range + 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link(1) . "'>&laquo; First</a>"; 
     if ($paged > 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link($paged - 1) . "'>&lsaquo; Previous</a>"; 

     for ($i = 1; $i <= $pages; $i++) { 
      if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) { 
       echo ($paged == $i) ? "<li><a href='" . get_pagenum_link($i) . "' class=\"current\">" . $i . "</a></li>" : "<li><a href='" . get_pagenum_link($i) . "' class=\"inactive\">" . $i . "</a></li>"; 
      } 
     } 

     if ($paged < $pages && $showitems < $pages) echo "<a href=\"" . get_pagenum_link($paged + 1) . "\">Next &rsaquo;</a>"; 
     if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) echo "<a href='" . get_pagenum_link($pages) . "'>Last &raquo;</a>"; 
     echo "</ul></div>\n"; 
    } 
} 
+0

謝謝諾曼,那工作...但在分頁第一個結果是在每一頁重複..? –

+0

@DhawalMhatre分頁不包含在問題中。所以應該有另一個問題,或者你可以使用'pagination'來定製循環。請參閱編輯。 – Noman

+0

srry諾曼...但分頁之前工作正常...此外,更新上述代碼後...分頁顯示相同的帖子在每一頁 –