![](/img/trans.png)
[英]Wordpress: Making first post full, while leaving the rest excerpts (with thumbnails!)
[英]WordPress: First post in full, others show excerpts
我已經設置了一個WordPress模板來完整顯示第一篇文章,其他內容摘錄為: http : //growthgroup.com/testing-blog
當您轉到帖子的第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?>
如果我理解正確,那么我想這可能會幫助您:
<?php if ($postcount == 1 && $paged == 1) : // if this is the first post & first page ?>
那應該只在首頁的第一篇文章上引起較高的條件。
Wordpress 3.1及更高版本使用模板部件。 這是獲取相同結果的更新方法(主題為二十十五)。 該代碼的第一部分位於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 )
) );
}
這也是自定義主題的改編版本:
<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;
?>
基本上,這組代碼的作用是,首先檢查它是否是ONLY帖子,在其中將摘錄設置為內容。 然后,如果不正確,則它將運行代碼“如果帖子數等於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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.