2013-10-20 54 views
0

我正在wordpress中实施一个月度存档,所以我使用date.php文件来显示每月的帖子。在这个文件中我有一些分页代码来获取月份和日期,但是我的循环只显示五个帖子,并且包含很多空的帖子,如果我做分页,每页有3篇文章,总页数仍然是5,即使分页工作。除此之外,结果还包含许多空的帖子(我的意思是它没有字段,但the_title返回1979年1月1日)。WordPress的Date.php返回只有五个帖子和许多空的帖子

我的循环如下所示(请不要介意外部位 - 它的自定义字段,以确定后整体外部存储):

$current_page = max(1, get_query_var('paged')); 
$posts = get_posts('cat=19,20&posts_per_page=3&monthnum='.$m.'&year='.$y.'&paged='.$current_page); 
    if (have_posts()) { 
     while (have_posts()) { 
      the_post(); 
      // 
      // Post Content here 
      ?> 

<?php $externalLink = do_shortcode('[cft key=external before_list="" after_list="" before_value="" after_value=""]'); ?> 
<?php if(strlen($externalLink) <= 1): ?> 
          <div class="title"> 
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> 
          <div class="time small"> 
          <?php if(get_the_time('Y') != '1970') the_time('F jS, Y'); //the_category('','multiple'); ?> 
          </div> 
          </div> 
<?php else: ?> 
          <div class="title"> 
          <a href="<?php echo $externalLink ?>" target="_blank" rel="bookmark" title="External Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> 
          <div class="time small"> 
          <?php if(get_the_time('Y') != '1970') the_time('F jS, Y'); // the_category('','multiple');?> 
          </div> 
          </div> 
<?php endif; ?>    

<?php 
     } // end while 
    } // end if 
?> 

整个代码包括呼叫逻辑在这里找到:https://gist.github.com/uansett/fd1183216ab980e1279a#file-date-php

回答

1

get_posts不会替换WordPress已经为页面选择的帖子。你会需要像(未经测试):

$posts = get_posts('cat=19,20&posts_per_page=3&monthnum='.$m.'&year='.$y.'&paged='.$current_page); 
foreach ($posts as $post) : setup_postdata($post); ?> 
    // Your code 
<?php endforeach; 
wp_reset_postdata(); // If you still want to access the originally selected posts ?> 

如果做不到这一点,你可以使用query_posts,这替代的WordPress已经选择的主题。虽然食典通常建议不要使用query_posts

TL; DR不要使用query_posts();

+0

非常好,这解决了我 - 现在没有更多的空帖子。干杯 – Alex