2014-04-02 46 views
0

我在category.php文件中有這樣的代碼,我需要通過ASC排序結果,因爲結果現在是相反的順序。如何在此代碼上對類別結果進行排序?

在此先感謝。

<ol class="search-results-list"> 

      <?php 
       // Return Event Items 
       $i = 0; 
       while (have_posts()) : the_post(); 

        if(get_post_type() == 'researcher') { 
         $i++; ?> 

         <li><strong><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong><br /> <?php print_excerpt(200); ?></li> 

        <?php } 

       endwhile;?> 

       <?php if($i == 0) { ?><li><?php _e('No results were found.', 'qns'); ?></li><?php } ?> 

      <!--END .search-results-list --> 
      </ol> 

回答

0

添加下面的代碼到你的functions.php文件

if (!function_exists('get_cat_id_by_slug')) { 
    function get_cat_id_by_slug ($get_slug) { 
     $CatId = get_term_by('slug', $get_slug, 'category'); 
     $CatId = $CatId->term_id; 
     return $CatId; 
    } 
} 

添加以下代碼到你category.php

<?php 

    $category_id = get_cat_id_by_slug('researcher'); 
    $args = 'cat=' . $category_id . 'order=asc'; 
    query_posts($args); 

    if (have_posts()) : 

     while (have_posts()) : the_post(); 
?> 
      <ol class="search-results-list"> 

       <li> 
        <strong> 
         <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> 
        </strong><br /> 
        <?php print_excerpt(200); ?> 
       </li> 

      </ol> 
<?php 
     endwhile; 

    else : 
?> 
     <ol class="search-results-list"> 
      <li><?php _e('No results were found.', 'qns'); ?></li> 
     </ol> 
<?php 
    endif; 
?> 
+0

非常感謝你,但我affraid不工作或有一種衝突。添加此代碼將整個網站完全變爲空白! – user3489056

+0

我測試了代碼,它工作正常。我不知道這是你的自定義摘錄還是別的東西沒有正確連接。 @ user3489056 –

相關問題