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

代碼實現WordPress上一篇和下一篇以及相關文章功能

站長經驗 尹華峰 瀏覽 評論來源:m.52dollars.com

  最近博主在弄WordPress網站,也體驗了很多不同的WordPress主題,其中不乏一些優秀主題,如博主以前介紹的知更鳥主題,確實是一個非常優秀的CMS主題,各個方面的SEO細節做得很完善,也因此造成了它在互聯網過于泛濫,帶來審美疲勞。

  以前博主搭建WordPress就是覺得網站越酷炫越好,現在博主越來越喜歡簡潔而功能強大的主題,為此也特意購買了一些比較欣賞的主題來體驗一番。但是,有些主題實在是過于簡潔,很多實用的功能也被刪除掉了,如有些主題文章頁沒有“上一篇和下一篇”,沒有相關文章版塊等。在我看來,這些是絕對不能省掉的,除了不利于搜索引擎優化,也同樣不利于讀者瀏覽體驗。當然這些小功能可以通過各種插件來實現,但是我試了不少的插件感覺不盡人意,為此博主尋找了一些方法通過代碼來完成這個小功能。

  實現WordPress上一篇和下一篇代碼方法

  在你的模板文件夾下找到single.php文件,編輯文章頁模板文件,在文章內容下方可插入以下代碼:

  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> 

  當然也可以對樣式進行布局,比如可以修改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. }  

  實現WordPress相關文章的三種方法

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

  方法一、標簽相關

  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.     // 獲取標簽列表
  8.     $tag_list[] .= $tag->term_id;
  9.   }
  10.   // 隨機獲取標簽列表中的一個標簽
  11.   $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
  12.   // 該方法使用 query_posts() 函數來調用相關文章,以下是參數列表
  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,                           // 顯示相關文章數量
  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>* 暫無相關文章</li>';
  30.   }
  31.   wp_reset_query();
  32. }
  33. else {
  34.   echo '<li>* 暫無相關文章</li>';
  35. }
  36. ?>
  37. </ul>

  PS:"不包括的分類ID" 指的是相關文章不顯示該分類下的文章,可自定義將NULL改成文章分類的ID即可,多個ID就用半角逗號隔開,滿足站長的多樣化需求。因為這里限制只顯示6篇相關文章,所以不管給 query_posts() 的參數 tag__in 賦多少個值,都是只顯示一個標簽下的6 篇文章,除非第一個標簽有1篇,第二個標簽有2篇,第三個有3篇...若當前文章有多個標簽對應,那么采取的做法是隨機獲取一個標簽的id,賦值給 tag__in 這個參數,獲取該標簽下的6篇文章。

  方法二、分類相關

  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>* 暫無相關文章</li>';
  22.   }
  23.   wp_reset_query();
  24. }
  25. else {
  26.   echo '<li>* 暫無相關文章</li>';
  27. }
  28. ?>
  29. </ul>

  此方法則是通過獲取該文章的分類id,然后獲取該分類下的6篇文章,來達到獲取相關文章的目的。

  方法三、作者相關

  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,               // 顯示相關文章數量
  9.         'orderby' => date,          // 按時間排序
  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>* 暫無相關文章</li>';
  22.   }
  23.   wp_reset_query();
  24. ?>
  25. </ul>

  此方法是獲取該文章作者的其他文章來充當相關文章,比較適合一些多個站長運營的網站。

  結語:博主之所以堅持給網站添加上一篇和下一篇以及相關文章,是因為博主是做SEO的,站在搜索引擎的角度來說,上一篇和下一篇以及相關文章的鏈接不僅可以增加搜索引擎蜘蛛抓取,而且也有利于網頁權重值傳遞。站在用戶的角度,相關的文章就好比是推薦,以及相關信息的進一步獲取,對讀者也是非常有利的。另外博主要補充的就是,關于相關文章實現方法,博主是建議選擇第一種,標簽往往更貼近主題。

    主站蜘蛛池模板: 佛坪县| 陆丰市| 宜丰县| 巴林右旗| 永宁县| 普兰县| 东港市| 赣州市| 长岭县| 托里县| 保定市| 乌苏市| 灵川县| 搜索| 扶沟县| 禄劝| 抚远县| 海南省| 平山县| 从化市| 壤塘县| 皮山县| 凌海市| 临江市| 公主岭市| 益阳市| 建始县| 昆明市| 恩平市| 大余县| 无锡市| 体育| 广平县| 米脂县| 衡山县| 抚州市| 贡山| 长沙县| 阿鲁科尔沁旗| 静宁县| 平舆县|