最近博主在弄WordPress網站,也體驗了很多不同的WordPress主題,其中不乏一些優秀主題,如博主以前介紹的知更鳥主題,確實是一個非常優秀的CMS主題,各個方面的SEO細節做得很完善,也因此造成了它在互聯網過于泛濫,帶來審美疲勞。
以前博主搭建WordPress就是覺得網站越酷炫越好,現在博主越來越喜歡簡潔而功能強大的主題,為此也特意購買了一些比較欣賞的主題來體驗一番。但是,有些主題實在是過于簡潔,很多實用的功能也被刪除掉了,如有些主題文章頁沒有“上一篇和下一篇”,沒有相關文章版塊等。在我看來,這些是絕對不能省掉的,除了不利于搜索引擎優化,也同樣不利于讀者瀏覽體驗。當然這些小功能可以通過各種插件來實現,但是我試了不少的插件感覺不盡人意,為此博主尋找了一些方法通過代碼來完成這個小功能。
實現WordPress上一篇和下一篇代碼方法
在你的模板文件夾下找到single.php文件,編輯文章頁模板文件,在文章內容下方可插入以下代碼:
-
<div class="nearbypost">
-
<div class="alignleft"><?php previous_post_link('« « %link'); ?></div>
-
<div class="alignright"><?php next_post_link('%link » » '); ?></div>
-
</div>
|
當然也可以對樣式進行布局,比如可以修改CSS樣式如下:
-
.alignleft {
-
float:left;
-
text-align:left;
-
margin-right:10px;
-
}
-
.alignright {
-
float:rightright;
-
text-align:rightright;
-
margin-left:10px;
-
}
|
實現WordPress相關文章的三種方法
同理,找到文章頁模板文件,在需要展示相關文章列表的地方添加如下代碼。
方法一、標簽相關
-
<ul id="tags_related">
-
<?php
-
global $post;
-
$post_tags = wp_get_post_tags($post->ID);
-
if ($post_tags) {
-
foreach ($post_tags as $tag) {
-
-
$tag_list[] .= $tag->term_id;
-
}
-
-
$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
-
-
$args = array(
-
'tag__in' => array($post_tag),
-
'category__not_in' => array(NULL),
-
'post__not_in' => array($post->ID),
-
'showposts' => 6,
-
'caller_get_posts' => 1
-
);
-
query_posts($args);
-
if (have_posts()) {
-
while (have_posts()) {
-
the_post(); update_post_caches($posts); ?>
-
<li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
-
<?php
-
}
-
}
-
else {
-
echo '<li>* 暫無相關文章</li>';
-
}
-
wp_reset_query();
-
}
-
else {
-
echo '<li>* 暫無相關文章</li>';
-
}
-
?>
-
</ul>
|
PS:"不包括的分類ID" 指的是相關文章不顯示該分類下的文章,可自定義將NULL改成文章分類的ID即可,多個ID就用半角逗號隔開,滿足站長的多樣化需求。因為這里限制只顯示6篇相關文章,所以不管給 query_posts() 的參數 tag__in 賦多少個值,都是只顯示一個標簽下的6 篇文章,除非第一個標簽有1篇,第二個標簽有2篇,第三個有3篇...若當前文章有多個標簽對應,那么采取的做法是隨機獲取一個標簽的id,賦值給 tag__in 這個參數,獲取該標簽下的6篇文章。
方法二、分類相關
-
<ul id="cat_related">
-
<?php
-
global $post;
-
$cats = wp_get_post_categories($post->ID);
-
if ($cats) {
-
$args = array(
-
'category__in' => array( $cats[0] ),
-
'post__not_in' => array( $post->ID ),
-
'showposts' => 6,
-
'caller_get_posts' => 1
-
);
-
query_posts($args);
-
if (have_posts()) {
-
while (have_posts()) {
-
the_post(); update_post_caches($posts); ?>
-
<li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
-
<?php
-
}
-
}
-
else {
-
echo '<li>* 暫無相關文章</li>';
-
}
-
wp_reset_query();
-
}
-
else {
-
echo '<li>* 暫無相關文章</li>';
-
}
-
?>
-
</ul>
|
此方法則是通過獲取該文章的分類id,然后獲取該分類下的6篇文章,來達到獲取相關文章的目的。
方法三、作者相關
-
<ul id="author_related">
-
<?php
-
global $post;
-
$post_author = get_the_author_meta( 'user_login' );
-
$args = array(
-
'author_name' => $post_author,
-
'post__not_in' => array($post->ID),
-
'showposts' => 6,
-
'orderby' => date,
-
'caller_get_posts' => 1
-
);
-
query_posts($args);
-
if (have_posts()) {
-
while (have_posts()) {
-
the_post(); update_post_caches($posts); ?>
-
<li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
-
<?php
-
}
-
}
-
else {
-
echo '<li>* 暫無相關文章</li>';
-
}
-
wp_reset_query();
-
?>
-
</ul>
|
此方法是獲取該文章作者的其他文章來充當相關文章,比較適合一些多個站長運營的網站。
結語:博主之所以堅持給網站添加上一篇和下一篇以及相關文章,是因為博主是做SEO的,站在搜索引擎的角度來說,上一篇和下一篇以及相關文章的鏈接不僅可以增加搜索引擎蜘蛛抓取,而且也有利于網頁權重值傳遞。站在用戶的角度,相關的文章就好比是推薦,以及相關信息的進一步獲取,對讀者也是非常有利的。另外博主要補充的就是,關于相關文章實現方法,博主是建議選擇第一種,標簽往往更貼近主題。