2012-07-29 133 views
1

我想在單一發布模式下返回2個最新帖子,但排除當前帖子。我做到了。問題是,它剛剛停止工作。它沒有改變代碼,它停止在服務器以及我的本地主機上。wp_get_recent_posts停止工作

下面的代碼:

<section id='recent_posts'> 

      <header class='recent_header'> 
       Recent posts 
      </header> 

      <?php 
       $id = $post->ID; 
       $recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id"); 

       foreach($recent_posts as $recent) { ?>       

        <article class='single_recent'> 
         <header> 
          <a href="<?php echo get_permalink($recent["ID"]); ?>"><?php echo $recent["post_title"]; ?></a> 
         </header> 
         <p> 
          <?php echo get_excerpt_by_id($recent["ID"]); ?> 
         </p> 
        </article> 

      <?php } ?> 

     </section> 

沒有人有一個解釋?

我嘗試刪除參數,仍然沒有。它返回一個空數組。

任何建議我應該使用哪些其他函數來實現相同的效果?

編輯:

<?php 

get_header(); 
get_sidebar(); 

?> 

     <?php the_post() ?> 

     <article class='post-single'> 

       <header class='post_header'> 

        <h1><?php the_title(); ?></h1> 

        <div class='post_header_bottom'> 
         <strong class='post_category'><?php echo get_the_category_list(', '); ?></strong> 
         <strong class='post_author'><span class='symbol'>U</span> by <?php the_author(); ?></strong> 
        </div> 

       </header> 

       <?php if (has_post_thumbnail()) : ?> 
       <figure class='post_single_image'> 
        <?php the_post_thumbnail(); ?> 
        <figcaption>No Will No Skill</figcaption> 
       </figure> 
       <?php endif; ?> 

       <div class='post_perex'> 
        <?php the_content(); ?> 
       </div> 

       <footer class='post_footer'> 

        <div class='post_footer_top'> 

         <div class='post_tags'> 
          <?php the_tags('', '', ''); ?> 
         </div> 

         <div class='post_time'> 
          <time datetime='<?php the_time('Y-m-d'); ?>' pubdate> 
           <span class='symbol'>P </span> 
           <?php relative_post_the_date(); ?> 
          </time> 
         </div> 

        </div> 

        <div class='post_share'> 

          <div class='share_show'> 
           <span class='symbol'>f</span> Like 
           | 
           <span class='symbol'>g</span> +1 
           | 
           <span class='symbol'>t</span> Tweet 

           <?php 
            if(function_exists('display_social4i')) 
             echo display_social4i("large","align-left"); 
           ?> 

          </div> 

         </div> 

       </footer> 

      </article> 

      <?php comments_template(); ?> 

      <section id='recent_posts'> 

       <header class='recent_header'> 
        Recent posts 
       </header> 

       <?php 
        global $post; 
        $id = $post->ID; 
        $qargs = array(
         'post__not_in'=> array($id), 
         'posts_per_page' => 2 
        ); 
        $recent_posts = new WP_Query($qargs); 

        if ($recent_posts->have_posts()) echo 'yes'; else echo 'nope'; 

        if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>       

         <article class='single_recent'> 
          <header> 
           <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
          </header> 
          <p> 
           <?php the_excerpt(); ?> 
          </p> 
         </article> 
       <?php endwhile;endif; ?> 
      </section> 

      <div class='space'></div> 
     </div> 


<?php 
get_footer(); 
?> 

回答

0

你錯過了你的循環,或至少在當前職位對象的全局實例。雖然我更喜歡自己使用循環,但可以使用後者。

變化:

$id = $post->ID; 
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id"); 

到:

global $post; 
$id = $post->ID; 
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id"); 

UPDATE:

環路可以在單個視圖中被用來抓住從默認query_posts對象信息。儘管它只是一篇文章,但循環通常用於填充the_content和其他信息。

由於wp_get_recent_posts()應該仍然返回一些沒有任何參數的結果(當然,除非是處理自定義帖子類型),所以在這種情況下ID應該是不相關的,這是正確的。

另一種解決辦法是嘗試讓使用WP_Query您最近的帖子:

<section id='recent_posts'> 

     <header class='recent_header'> 
      Recent posts 
     </header> 

     <?php 
      global $post; 
      $id = $post->ID; 
      $qargs = array(
       'post__not_in'=> array($id), 
       'posts_per_page' => 2 
      ); 
      $recent_posts = new WP_Query($qargs); 

      if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>       

       <article class='single_recent'> 
        <header> 
         <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
        </header> 
        <p> 
         <?php the_excerpt(); ?> 
        </p> 
       </article> 
     <?php endwhile;endif; ?> 
</section> 

WP_Query將默認爲標準的「排序依據」 =>「日期」和「秩序」 =>「DESC」參數,所以這些不需要明確說明(儘管如果你願意,你可以添加它們)。

如上所述,在我的評論中,我不確定您是否想要最近的帖子,或者如果您想查詢最近的自定義帖子類型。如果最近發佈的帖子是「發佈」類型,而這正是您想要的,那麼這是WP_Query的「post_type」參數以及wp_get_recent_posts的默認值。

否則,你將需要明確聲明「post_type」參數,使得$ qargs樣子:

$qargs = array(
    'post__not_in'=> array($id), 
    'posts_per_page' => 2, 
    'post_type' => array('post', 'custom_post_type_slug', ...) 
); 

只是爲了檢查,你還可以設置「post_type」到「所有」,如果你想以確保返回SOMETHING。如果WP_Query或wp_get_recent_posts沒有將'post_type'設置爲'all'返回任何內容,則問題會更大,我需要查看single.php模板中的所有代碼。

希望這會有所幫助。

新的更新:

嘗試乾脆替代註釋掉的代碼塊:

wp_get_archives(array('type'=>'postbypost')); 

如果不工作,那麼我新鮮的想法。你的文件主機的一端可能發生了一些事情,這可能可以解釋一件事發生的事情。檢查並看看他們是否有關於這種活動的任何公告。我知道許多文件主機正在取代PHP4和更舊版本的MySQL,這可能會導致一些未解決的問題。

我會嘗試創建一個新的子域與乾淨,單獨安裝的Wordpress和你的主題的副本。一旦設置好了,只需創建一個新帖子,看看是否出現同樣的問題。

如果是這樣,那麼在您的single.php文件中註釋掉大量代碼(可能從get_sidebar註釋掉第一個<article>...</article>塊開始),並計算可能彈出的任何錯誤。如果我們工作的代碼塊突然開始填充,然後發佈阻止它發生的代碼塊,我會盡力與您一起工作(也就是說,如果您仍然遇到問題)。否則,如果幹淨安裝修復了您的問題,那麼它可能是wp_options表中的某種差異。我會開始合併表(首先wp_posts,然後wp_postmeta,然後wp_terms ....),直到問題再次彈出。

不幸的是,我認爲徹底的測試可能是爲了解決這個異常問題。對不起,我沒有一個純粹的代碼解決方案。這是一個很奇怪的問題,你正在經歷。讓我張貼,我會盡我所能幫助。

+0

爲什麼我會在單個帖子視圖中使用循環? 順便提一下,帖子ID不是問題。它得到的ID很好。問題是該函數返回零個帖子(即使沒有參數) – 2012-07-30 10:13:19

+0

你是正確的,該函數應該仍然返回一個結果數組,無論ID如何。我很抱歉。出於好奇,我們是在處理標準職位?或者你是否試圖查詢自定義帖子?因爲如果您想要查詢最近的自定義帖子或頁面,則需要設置'post_type'參數。 – maiorano84 2012-07-30 16:58:51

+0

是的,我想返回標準帖子。我試過你的代碼,它仍然沒有返回。我將嘗試編輯帖子並將其中的整個single.php代碼粘貼。 – 2012-07-30 23:22:12