2012-05-28 59 views
0

我已經創建了一個自定義帖子類型,以在我的投資組合網站上從我的博客手工製作摘錄。我有一個內容窗口,一個鏈接和一個帖子類型的精選圖片,我稱之爲blogwordpress自定義帖子類型不能改變顯示順序

問題是,無論我嘗試什麼,帖子都會顯示爲最新的,但我想先顯示最新的帖子。這裏的query_posts()電話:

<?php query_posts('post_type=blog&order=ASC'); ?>

但我也嘗試更復雜的查詢,如:

<?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC')); ?>

我的完整模板文件看起來像: ` 「>

<div class="sliderContent"> 
     <!--first loop--> 
     <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
     <?php the_content(__('Read More &raquo;', THEMENAME)); ?> 
     <?php endwhile; else: ?> 
     <p><?php _e('Nothing found.', THEMENAME); ?></p> 
     <?php endif; ?> 

     <!--second loop, displays custom post type--> 
     <?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC')); ?> 
     <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
     <div class="contenttile"> 
      <p><a href="<?php echo get_post_meta($post->ID, 'Permalink', true); ?>"><?php the_post_thumbnail('medium'); ?></a></p> 
      <h2><?php the_title(); ?></h2> 

      <?php the_content(__('Read More &raquo;', THEMENAME)); ?> 
     </div> 
     <?php endwhile; else: ?> 
     <p><?php _e('Nothing found.', THEMENAME); ?></p> 
     <?php endif; ?> 
    </div> 
</div> 
<!-- content end --> 

<?php } ?> 

`

所以我顯示來自該模板應用頁面的內容,那麼我顯示我的自定義後的類型。

感謝您的幫助,我很難過!

+0

正如另一位回答者指出的,在這裏我選擇了'ASC'作爲排序順序,但是我可以將其更改爲'DESC'並且它沒有區別。事實上,它有點像'query_posts()'忽略查詢的每一部分,但'post_type'?那可能嗎? – catdotgif

回答

1

您的查詢按升序排序。升序從最老到最新。如果您希望最新到最舊,您需要DESCENDING命令。另外,你應該儘可能避免使用query_posts,因爲它會修改默認的Wordpress循環。

你的第二個查詢並不比第一個查詢更精細。唯一的區別是你使用數組而不是字符串來定義查詢參數(數組可以說是正確的方法),並且你正在設置orderby參數。

最後,默認順序按降序排列(最新到最舊),所以理論上甚至不需要通過參數定義順序和順序。

試試這個:

<!--second loop, displays custom post type--> 
    <?php 
    $args = array('post_type' => 'blog', 'orderby'=>'date','order'=>'DESC'); 
    /*Consider changing to: $args = array('post_type' => 'blog');*/ 
    $query = new WP_Query($args); 
    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); 
    ?> 
    <div class="contenttile"> 
     <p><a href="<?php echo get_post_meta($post->ID, 'Permalink', true); ?>"><?php the_post_thumbnail('medium'); ?></a></p> 
     <h2><?php the_title(); ?></h2> 

     <?php the_content(__('Read More &raquo;', THEMENAME)); ?> 
    </div> 
    <?php endwhile; else: ?> 
    <p><?php _e('Nothing found.', THEMENAME); ?></p> 
    <?php 
    endif; 
    wp_reset_postdata(); 
    ?> 
</div> 

+0

我應該澄清一點,我已將順序設置爲ASC或DESC,但不起作用。 – catdotgif

0

嗯,majorano84提到,經過進一步的閱讀query_posts()完全不使用(因爲我想這是更多的工作正確的函數服務器?)

取而代之,我使用了get_posts(),並且按照首選順序顯示帖子,而我沒有做任何進一步的努力:

 <?php 
      $args = array('post_type' => 'blog'); 
      $lastposts = get_posts($args); 
      foreach($lastposts as $post) : setup_postdata($post); ?> 
       <div class="contenttile"> 
         <p><a href="<?php echo get_post_meta($post->ID, 'Permalink', true); ?>"><?php the_post_thumbnail('medium'); ?></a></p> 
         <h2><?php the_title(); ?></h2> 
         <?php the_content(); ?> 
       </div> 
     <?php endforeach; ?> 

`

所以我要調用的答案,因爲它解決了我是有這個問題。謝謝!

相關問題