2012-07-29 131 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