最近博主在弄WordPress網(wǎng)站,也體驗(yàn)了很多不同的WordPress主題,其中不乏一些優(yōu)秀主題,如博主以前介紹的知更鳥(niǎo)主題,確實(shí)是一個(gè)非常優(yōu)秀的CMS主題,各個(gè)方面的SEO細(xì)節(jié)做得很完善,也因此造成了它在互聯(lián)網(wǎng)過(guò)于泛濫,帶來(lái)審美疲勞。
以前博主搭建WordPress就是覺(jué)得網(wǎng)站越酷炫越好,現(xiàn)在博主越來(lái)越喜歡簡(jiǎn)潔而功能強(qiáng)大的主題,為此也特意購(gòu)買(mǎi)了一些比較欣賞的主題來(lái)體驗(yàn)一番。但是,有些主題實(shí)在是過(guò)于簡(jiǎn)潔,很多實(shí)用的功能也被刪除掉了,如有些主題文章頁(yè)沒(méi)有“上一篇和下一篇”,沒(méi)有相關(guān)文章版塊等。在我看來(lái),這些是絕對(duì)不能省掉的,除了不利于搜索引擎優(yōu)化,也同樣不利于讀者瀏覽體驗(yàn)。當(dāng)然這些小功能可以通過(guò)各種插件來(lái)實(shí)現(xiàn),但是我試了不少的插件感覺(jué)不盡人意,為此博主尋找了一些方法通過(guò)代碼來(lái)完成這個(gè)小功能。
實(shí)現(xiàn)WordPress上一篇和下一篇代碼方法
在你的模板文件夾下找到single.php文件,編輯文章頁(yè)模板文件,在文章內(nèi)容下方可插入以下代碼:
-
<div class="nearbypost">
-
<div class="alignleft"><?php previous_post_link('« « %link'); ?></div>
-
<div class="alignright"><?php next_post_link('%link » » '); ?></div>
-
</div>
|
當(dāng)然也可以對(duì)樣式進(jìn)行布局,比如可以修改CSS樣式如下:
-
.alignleft {
-
float:left;
-
text-align:left;
-
margin-right:10px;
-
}
-
.alignright {
-
float:rightright;
-
text-align:rightright;
-
margin-left:10px;
-
}
|
實(shí)現(xiàn)WordPress相關(guān)文章的三種方法
同理,找到文章頁(yè)模板文件,在需要展示相關(guān)文章列表的地方添加如下代碼。
方法一、標(biāo)簽相關(guān)
-
<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>* 暫無(wú)相關(guān)文章</li>';
-
}
-
wp_reset_query();
-
}
-
else {
-
echo '<li>* 暫無(wú)相關(guān)文章</li>';
-
}
-
?>
-
</ul>
|
PS:"不包括的分類(lèi)ID" 指的是相關(guān)文章不顯示該分類(lèi)下的文章,可自定義將NULL改成文章分類(lèi)的ID即可,多個(gè)ID就用半角逗號(hào)隔開(kāi),滿(mǎn)足站長(zhǎng)的多樣化需求。因?yàn)檫@里限制只顯示6篇相關(guān)文章,所以不管給 query_posts() 的參數(shù) tag__in 賦多少個(gè)值,都是只顯示一個(gè)標(biāo)簽下的6 篇文章,除非第一個(gè)標(biāo)簽有1篇,第二個(gè)標(biāo)簽有2篇,第三個(gè)有3篇...若當(dāng)前文章有多個(gè)標(biāo)簽對(duì)應(yīng),那么采取的做法是隨機(jī)獲取一個(gè)標(biāo)簽的id,賦值給 tag__in 這個(gè)參數(shù),獲取該標(biāo)簽下的6篇文章。
方法二、分類(lèi)相關(guān)
-
<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>* 暫無(wú)相關(guān)文章</li>';
-
}
-
wp_reset_query();
-
}
-
else {
-
echo '<li>* 暫無(wú)相關(guān)文章</li>';
-
}
-
?>
-
</ul>
|
此方法則是通過(guò)獲取該文章的分類(lèi)id,然后獲取該分類(lèi)下的6篇文章,來(lái)達(dá)到獲取相關(guān)文章的目的。
方法三、作者相關(guān)
-
<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>* 暫無(wú)相關(guān)文章</li>';
-
}
-
wp_reset_query();
-
?>
-
</ul>
|
此方法是獲取該文章作者的其他文章來(lái)充當(dāng)相關(guān)文章,比較適合一些多個(gè)站長(zhǎng)運(yùn)營(yíng)的網(wǎng)站。
結(jié)語(yǔ):博主之所以堅(jiān)持給網(wǎng)站添加上一篇和下一篇以及相關(guān)文章,是因?yàn)椴┲魇亲鯯EO的,站在搜索引擎的角度來(lái)說(shuō),上一篇和下一篇以及相關(guān)文章的鏈接不僅可以增加搜索引擎蜘蛛抓取,而且也有利于網(wǎng)頁(yè)權(quán)重值傳遞。站在用戶(hù)的角度,相關(guān)的文章就好比是推薦,以及相關(guān)信息的進(jìn)一步獲取,對(duì)讀者也是非常有利的。另外博主要補(bǔ)充的就是,關(guān)于相關(guān)文章實(shí)現(xiàn)方法,博主是建議選擇第一種,標(biāo)簽往往更貼近主題。