2011-04-06 28 views
0

當我爲最近的帖子寫下面的代碼,然後在我爲下一個和上一個鏈接編寫代碼之後,那個函數會給我同一個帖子的鏈接。如果我評論「$ tags = wp_get_post_tags($ post-> ID);」這行然後它會打印下一篇文章鏈接。我該如何解決這個錯誤?請幫幫我。next_post_link()函數在wordpress中給出相同的帖子URL

   <?php 
        //for use in the loop, list 5 post titles related to first tag on current post 

        $tags = wp_get_post_tags($post->ID); 
        if ($tags) { 
         echo '<div class="articlecontent font16 bold fontgray">Related Posts</div>'; 
         $first_tag = $tags[0]->term_id; 
         $args=array(
         'tag__in' => array($first_tag), 
         'post__not_in' => array($post->ID), 
         'showposts'=>5, 
         'caller_get_posts'=>1 
         ); 
         $my_query = new WP_Query($args); 
         if($my_query->have_posts()) { 
         echo '<div class="articlecontent"><ul>'; 
         while ($my_query->have_posts()) : $my_query->the_post(); ?> 
          <li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> 
          <?php 
         endwhile; 
         echo "</ul></div>"; 
         } 
        } 
       ?> 

       <div class="nextprevbar"> 
        <div class="prevtopic"><?php 

         previous_post_link('%link', '<img border="0" alt="" src="'.get_template_directory_uri().'/images/prev-bullet.gif">' . _x('&nbsp;', 'Previous post link', 'twentyten') . ' %title'); ?></div> 
        <div class="nexttopic"><?php next_post_link('%link', '%title &nbsp;<img border="0" alt="" src="'.get_template_directory_uri().'/images/next-bullet.gif">' . _x('&nbsp;', 'Next post link', 'twentyten') . '</span>'); ?></div> 
       </div> 

回答

2

問題必須在$post變量中。執行這行之前:

while ($my_query->have_posts()) : $my_query->the_post(); ?> 

$post VAR保存當前數據後(我想這個代碼是內部的single.php?)。但是,在這一行以及循環內部,$post var會一個接一個地保存您最近發佈的各種帖子(您在撥打the_post()時設置了$post變量)。

在該循環之後(在endwhile下方),$post將保存在該循環中檢索到的最後一個帖子的數據。

previous_post_link()next_post_link()需要訪問$post當前職位的參考,但他們正在爲參考您最近的帖子的最後一個職位,而不是通過後您的用戶讀取。

我不知道這個頁面的html結構是什麼,但是我會在導航鏈接(next,previous posts)之後放置最近的帖子列表。如果我是對的,這將解決問題,並且我認爲它在語義上更清晰。

或者你也可以試試這個:

加入這一行:

$currentPost = clone $post; 

前:

$my_query = new WP_Query($args); 

並添加此行:

<?php $post = $currentPost; ?> 

致電前下一個和上一個帖子鏈接的功能。

相關問題