首頁 >CMS教程 >&#&按 >怎麼為WordPress上下篇文章連結加入縮圖

怎麼為WordPress上下篇文章連結加入縮圖

藏色散人
藏色散人轉載
2021-05-12 17:18:262859瀏覽

下面由WordPress教學專欄跟大家介紹怎麼為WordPress上下篇文章連結加入縮圖,希望對需要的朋友有幫助!

為WordPress上下篇文章鏈接添加縮圖

大部分WordPress主題都會在正文下面添加上下篇文章的鏈接,可以通過下面的代碼給這個鏈接再加個縮圖,讓其更醒目。

將下面程式碼新增至正文模板檔案的適當位置即可。

程式碼一

預設呼叫文章100×100的特色圖。

<div id="post-nav" class="navigation">
<?php $prevPost = get_previous_post(true);
if($prevPost) ?>
<div class="nav-box previous">
<?php previous_post_link(&#39;&laquo; &laquo; Previous Post:&#39;, &#39;yes&#39;); ?>
<?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) );?>
<?php previous_post_link(&#39;%link&#39;,"$prevthumbnail <p>%title</p>", TRUE); ?>
</div>
<?php $nextPost = get_next_post(true);
if($nextPost) ?>
<div class="nav-box next" style="float:right;">
<?php previous_post_link(&#39;&raquo; &raquo; Next Post:&#39;, &#39;yes&#39;); ?>
<?php $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) ); ?>
<?php next_post_link(&#39;%link&#39;,"$nextthumbnail <p>%title</p>", TRUE); ?>
</div>
<?php ?>
</div>

 

配對樣式

#post-nav{clear: both; height: 100px; margin: 0 0 70px;}
#post-nav .nav-box{background: #e9e9e9; padding: 10px;}
#post-nav img{float: left; margin: 0 10px 0 0;}
 
#post-nav p{margin: 0 10px; font-size: 11px; vertical-align: middle;}
#post-nav .previous{float: left; vertical-align: middle; width: 300px; height: 120px;}
#post-nav .next{float: right; width: 300px; height: 120px;}

程式碼二

除了呼叫特色圖片,並顯示文章發表時間,稍加修改還可以添加更多的文章信息,包括自訂縮圖、文章簡短等。

<div id="post-nav">
<?php $prevPost = get_previous_post(true);
if($prevPost) {
$args = array(
&#39;posts_per_page&#39; => 1,
&#39;include&#39; => $prevPost->ID
);
$prevPost = get_posts($args);
foreach ($prevPost as $post) {
setup_postdata($post);
?>
<div class="post-previous">
<a class="previous" href="<?php the_permalink(); ?>">&laquo; Previous Story</a>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(&#39;thumbnail&#39;); ?></a>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<small><?php the_date(&#39;F j, Y&#39;); ?></small>
</div>
<?php
wp_reset_postdata();
} //end foreach
} // end if
 
$nextPost = get_next_post(true);
if($nextPost) {
$args = array(
&#39;posts_per_page&#39; => 1,
&#39;include&#39; => $nextPost->ID
);
$nextPost = get_posts($args);
foreach ($nextPost as $post) {
setup_postdata($post);
?>
<div class="post-next">
<a class="next" href="<?php the_permalink(); ?>">Next Story &raquo;</a>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(&#39;thumbnail&#39;); ?></a>
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<small><?php the_date(&#39;F j, Y&#39;); ?></strong>
</div>
<?php
wp_reset_postdata();
} //end foreach
} // end if
?>
</div>

以上是怎麼為WordPress上下篇文章連結加入縮圖的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:zmingcx.com。如有侵權,請聯絡admin@php.cn刪除