2013-10-07 57 views
0

我已經成立了一個WordPress的模板,以顯示完整的第一篇文章,其餘爲摘錄:http://growthgroup.com/testing-blogWordPress的:第一篇文章中滿,另一些則摘錄

當你去到第2頁的帖子,它顯示第一篇文章全部,其他人也作爲摘錄,但我只希望最近發佈的文章是全文,所有其他文章是節選。

有沒有辦法解決這個問題?這裏是我使用的代碼:

<?php 
// query to set the posts per page to 5 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$args = array('posts_per_page' => 5, 'paged' => $paged); 
query_posts($args); ?> 

<?php if (have_posts()) : ?> 
<?php $postcount = 0; // Initialize the post counter ?> 
<?php while (have_posts()) : the_post(); //start the loop ?> 
<?php $postcount++; //add 1 to the post counter ?> 


<?php if ($postcount == 1) : // if this is the first post ?> 
<div class="post"> 
<h3 class="post-title"><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title(); ?></a></h3> 
<span class="author-date-blog"><?php the_author();?> | <?php the_date('d M Y');?></span> 
<div class="excerpt"> 
<?php the_content();?> 
<div class="read-more"> 
<a href="<?php the_permalink();?>#disqus_thread">Leave a Comment >></a> 
</div> 
</div> 
</div> 



<?php else : //if this is NOT the first post ?> 
<div class="post"> 
<div class="thumb"> 
<a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_post_thumbnail('blog-thumb');?></a> 
</div> 
<h3 class="post-title"><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title(); ?></a></h3> 
<span class="author-date-blog"><?php the_author();?> | <?php the_date('d M Y');?></span> 
<div class="excerpt"> 
<?php the_excerpt(); ?> 
<div class="read-more"> 
<a href="<?php the_permalink();?>">Read More >></a> 
</div> 
</div> 
</div> 
<?php endif; endwhile; //end of the check for first post - other posts?> 

回答

2

如果我理解正確的話,我想這可能會做你以後:

<?php if ($postcount == 1 && $paged == 1) : // if this is the first post & first page ?> 

這應該使第一上部條件火只有第一頁的帖子。

+0

完美 - 謝謝! – rossautomatica

0

WordPress的版本3.1,並利用模板零件。這裏有一個更新的方法來獲得相同的結果(與Twentyfifteen主題)。代碼的第一部分位於index.php中,這裏的代碼位於content.php中。

if ($wp_query->current_post == 0) { 
    /* translators: %s: Name of current post */ 
    the_content(sprintf(
    __('Continue reading %s', 'twentyfifteen'), 
    the_title('<span class="screen-reader-text">', '</span>', false) 
    )); 

} 

else { 

    /* translators: %s: Name of current post */ 
    the_excerpt(sprintf(
    __('Continue reading %s', 'twentyfifteen'), 
    the_title('<span class="screen-reader-text">', '</span>', false) 
    )); 

} 

Source

下面是一個自定義主題改編版本太:

<div class="entry-content"> 
    <?php 
    if (is_single()) : 
      the_content(); 
    //} 
    elseif ($wp_query->current_post == 0) : 
     /* translators: %s: Name of current post */ 
     the_content(); 
    else : 
     the_excerpt(); 
     echo '<a class="read_more" href="' . esc_url(get_permalink()) . '">' . __('Read more', 'imelton') . '</a>'; 
    endif; 
    ?> 

基本上,這套代碼確實是第一次檢查,看它是否是唯一的帖子,其中它設置了內容的摘錄。然後,如果不是這樣,那麼它將運行「如果帖子數等於0,然後顯示內容的代碼,如果兩個規則不符合,則顯示摘錄。」

值得注意的是,在自定義版本中,標題有自己的代碼行。

<header class="entry-header"> 
    <?php 
    if (is_single()) : 
     the_title('<h1 class="entry-title">', '</h1>'); 
    else : 
     the_title(sprintf('<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url(get_permalink())), '</a></h2>'); 
    endif; 
    ?> 
<div class="post_date"><?php the_date('l, F j, Y - h:i', '', ''); ?></div> 
相關問題