欧美日产国产成人免费图片,国产精品久久久久av蜜臀,欧美韩国日本一区,在线精品亚洲一区二区不卡

代碼實(shí)現(xiàn)WordPress上一篇和下一篇以及相關(guān)文章功能

站長(zhǎng)經(jīng)驗(yàn) 尹華峰 瀏覽 評(píng)論來(lái)源:www.52dollars.com

  最近博主在弄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)買了一些比較欣賞的主題來(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)容下方可插入以下代碼:

  1. <div class="nearbypost">    
  2. <div class="alignleft"><?php previous_post_link('« « %link'); ?></div>    
  3. <div class="alignright"><?php next_post_link('%link  » » '); ?></div>    
  4. </div> 

  當(dāng)然也可以對(duì)樣式進(jìn)行布局,比如可以修改CSS樣式如下:

  1. .alignleft {  
  2.  float:left;  
  3.  text-align:left;  
  4.  margin-right:10px;  
  5. }  
  6. .alignright {  
  7.  float:rightright;  
  8.  text-align:rightright;  
  9.  margin-left:10px;  
  10. }  

  實(shí)現(xiàn)WordPress相關(guān)文章的三種方法

  同理,找到文章頁(yè)模板文件,在需要展示相關(guān)文章列表的地方添加如下代碼。

  方法一、標(biāo)簽相關(guān)

  1. <ul id="tags_related">
  2. <?php
  3. global $post;
  4. $post_tags = wp_get_post_tags($post->ID);
  5. if ($post_tags) {
  6.   foreach ($post_tags as $tag) {
  7.     // 獲取標(biāo)簽列表
  8.     $tag_list[] .= $tag->term_id;
  9.   }
  10.   // 隨機(jī)獲取標(biāo)簽列表中的一個(gè)標(biāo)簽
  11.   $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
  12.   // 該方法使用 query_posts() 函數(shù)來(lái)調(diào)用相關(guān)文章,以下是參數(shù)列表
  13.   $args = array(
  14.         'tag__in' => array($post_tag),
  15.         'category__not_in' => array(NULL),  // 不包括的分類ID
  16.         'post__not_in' => array($post->ID),
  17.         'showposts' => 6,                           // 顯示相關(guān)文章數(shù)量
  18.         'caller_get_posts' => 1
  19.     );
  20.   query_posts($args);
  21.   if (have_posts()) {
  22.     while (have_posts()) {
  23.       the_post(); update_post_caches($posts); ?>
  24.     <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
  25. <?php
  26.     }
  27.   }
  28.   else {
  29.     echo '<li>* 暫無(wú)相關(guān)文章</li>';
  30.   }
  31.   wp_reset_query();
  32. }
  33. else {
  34.   echo '<li>* 暫無(wú)相關(guān)文章</li>';
  35. }
  36. ?>
  37. </ul>

  PS:"不包括的分類ID" 指的是相關(guān)文章不顯示該分類下的文章,可自定義將NULL改成文章分類的ID即可,多個(gè)ID就用半角逗號(hào)隔開(kāi),滿足站長(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篇文章。

  方法二、分類相關(guān)

  1. <ul id="cat_related">
  2. <?php
  3. global $post;
  4. $cats = wp_get_post_categories($post->ID);
  5. if ($cats) {
  6.     $args = array(
  7.           'category__in' => array$cats[0] ),
  8.           'post__not_in' => array$post->ID ),
  9.           'showposts' => 6,
  10.           'caller_get_posts' => 1
  11.       );
  12.   query_posts($args);
  13.   if (have_posts()) {
  14.     while (have_posts()) {
  15.       the_post(); update_post_caches($posts); ?>
  16.   <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
  17. <?php
  18.     }
  19.   }
  20.   else {
  21.     echo '<li>* 暫無(wú)相關(guān)文章</li>';
  22.   }
  23.   wp_reset_query();
  24. }
  25. else {
  26.   echo '<li>* 暫無(wú)相關(guān)文章</li>';
  27. }
  28. ?>
  29. </ul>

  此方法則是通過(guò)獲取該文章的分類id,然后獲取該分類下的6篇文章,來(lái)達(dá)到獲取相關(guān)文章的目的。

  方法三、作者相關(guān)

  1. <ul id="author_related">
  2. <?php
  3.   global $post;
  4.   $post_author = get_the_author_meta( 'user_login' );
  5.   $args = array(
  6.         'author_name' => $post_author,
  7.         'post__not_in' => array($post->ID),
  8.         'showposts' => 6,               // 顯示相關(guān)文章數(shù)量
  9.         'orderby' => date,          // 按時(shí)間排序
  10.         'caller_get_posts' => 1
  11.     );
  12.   query_posts($args);
  13.   if (have_posts()) {
  14.     while (have_posts()) {
  15.       the_post(); update_post_caches($posts); ?>
  16.   <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
  17. <?php
  18.     }
  19.   }
  20.   else {
  21.     echo '<li>* 暫無(wú)相關(guān)文章</li>';
  22.   }
  23.   wp_reset_query();
  24. ?>
  25. </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)重值傳遞。站在用戶的角度,相關(guān)的文章就好比是推薦,以及相關(guān)信息的進(jìn)一步獲取,對(duì)讀者也是非常有利的。另外博主要補(bǔ)充的就是,關(guān)于相關(guān)文章實(shí)現(xiàn)方法,博主是建議選擇第一種,標(biāo)簽往往更貼近主題。

    主站蜘蛛池模板: 民乐县| 招远市| 民丰县| 锦州市| 江孜县| 彰武县| 阳曲县| 延川县| 上饶县| 安吉县| 宝应县| 印江| 宜君县| 荣成市| 台东市| 襄城县| 阿图什市| 祁连县| 柳江县| 乌拉特后旗| 贵州省| 宁化县| 黄龙县| 福海县| 阜阳市| 原阳县| 五指山市| 阿勒泰市| 承德市| 涿鹿县| 孝昌县| 微山县| 黑龙江省| 绥化市| 西畴县| 泰安市| 呼伦贝尔市| 怀来县| 德阳市| 卢湾区| 兴义市|