WordPress代碼實現(xiàn)相關(guān)文章的幾種方法

    一篇十分優(yōu)秀的博文

    來源于:

    http://www.nafanlong.com/2473.html

    一、標題/縮略圖樣式

    1.添加標題列表樣式的相關(guān)文章
    WordPress代碼實現(xiàn)相關(guān)文章的幾種方法

    這種相關(guān)文章的獲取思路是:Tags標簽相關(guān)>同分類下文章,也就是說,先獲取標簽相同的文章,如果還達不到數(shù)量,就調(diào)用該分類下的文章補足。

    將下面的代碼添加到 single.php 要顯示相關(guān)文章的位置即可:

    <h3>相關(guān)文章h3>
    <ul class="related_posts">
    
    $post_num = 8;
    $exclude_id = $post->ID;
    $posttags = get_the_tags(); $i = 0;
    if ( $posttags ) {
    	$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
    	$args = array(
    		'post_status' => 'publish',
    		'tag__in' => explode(',', $tags),
    		'post__not_in' => explode(',', $exclude_id),
    		'caller_get_posts' => 1,
    		'orderby' => 'comment_date',
    		'posts_per_page' => $post_num,
    	);
    	query_posts($args);
    	while( have_posts() ) { the_post(); ?>
    		<li><a rel="bookmark" href="" title="" target="_blank"> the_title(); ?>a>li>
    	
    		$exclude_id .= ',' . $post->ID; $i ++;
    	} wp_reset_query();
    }
    if ( $i < $post_num ) {
    	$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
    	$args = array(
    		'category__in' => explode(',', $cats),
    		'post__not_in' => explode(',', $exclude_id),
    		'caller_get_posts' => 1,
    		'orderby' => 'comment_date',
    		'posts_per_page' => $post_num - $i
    	);
    	query_posts($args);
    	while( have_posts() ) { the_post(); ?>
    		<li><a rel="bookmark" href=""  title="" target="_blank"> the_title(); ?>a>li>
    	 $i++;
    	} wp_reset_query();
    }
    if ( $i  == 0 )  echo '
    沒有相關(guān)文章!
    ';
    ?>
    ul>
    

    PS:第四行$post_num = 8;表示顯示8篇文章,請根據(jù)自己的需要修改。顯示樣式需要自己寫css,可以參考一下下面的:

    .related_posts{margin-top:5px;margin-left:10px;margin-bottom:20px;height:150px;list-style-position: inside;}
    .related_posts li{margin-left:20px;color:#444;list-style:circle;font-size:14px;line-height:26px;padding:0 0 0 5px;float:left;width:45%;padding-right:9px;overflow:hidden;white-space:nowrap;text-overflow:ellipsis}
    
    2.添加含縮略圖的相關(guān)文章
    WordPress代碼實現(xiàn)相關(guān)文章的幾種方法

    1)在主題的 functions.php 的最后一個 ?> 前添加下面的代碼:

    //添加特色縮略圖支持
    if ( function_exists('add_theme_support') )add_theme_support('post-thumbnails');
     
    //輸出縮略圖地址 From wpdaxue.com
    function post_thumbnail_src(){
        global $post;
    	if( $values = get_post_custom_values("thumb") ) {	//輸出自定義域圖片地址
    		$values = get_post_custom_values("thumb");
    		$post_thumbnail_src = $values [0];
    	} elseif( has_post_thumbnail() ){    //如果有特色縮略圖,則輸出縮略圖地址
            $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
    		$post_thumbnail_src = $thumbnail_src [0];
        } else {
    		$post_thumbnail_src = '';
    		ob_start();
    		ob_end_clean();
    		$output = preg_match_all('//i', $post->post_content, $matches);
    		$post_thumbnail_src = $matches [1] [0];   //獲取該圖片 src
    		if(empty($post_thumbnail_src)){	//如果日志中沒有圖片,則顯示隨機圖片
    			$random = mt_rand(1, 10);
    			echo get_bloginfo('template_url');
    			echo '/images/pic/'.$random.'.jpg';
    			//如果日志中沒有圖片,則顯示默認圖片
    			//echo '/images/default_thumb.jpg';
    		}
    	};
    	echo $post_thumbnail_src;
    }
    

    PS:上面的代碼主要是獲取圖片鏈接,獲取的順序是:

    自定義字段為:thumb 的圖片>特色縮略圖>文章第一張圖片>隨機圖片/默認圖片;

    隨機圖片:請制作10張圖片,放在現(xiàn)用主題文件夾下的 images/pic/ 目錄,圖片為jpg格式,并且使用數(shù)字 1-10命名,比如 1.jpg;

    如果你不想用隨機圖片,請將 倒數(shù)第5行 前面的“//”去掉,然后給 倒數(shù)第7、9行 前面添加“//”注銷,并且在現(xiàn)用主題的 /images/ 目錄下添加一張名字為 default_thumb.jpg 的默認圖片,這樣,就會顯示默認圖片。

    2)將下面的代碼添加到 single.php 要顯示相關(guān)文章的位置:

    <h3>相關(guān)文章h3>
    <ul class="related_img">
    
    $post_num = 4;
    $exclude_id = $post->ID;
    $posttags = get_the_tags(); $i = 0;
    if ( $posttags ) {
    	$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
    	$args = array(
    		'post_status' => 'publish',
    		'tag__in' => explode(',', $tags),
    		'post__not_in' => explode(',', $exclude_id),
    		'caller_get_posts' => 1,
    		'orderby' => 'comment_date',
    		'posts_per_page' => $post_num
    	);
    	query_posts($args);
    	while( have_posts() ) { the_post(); ?>
    		<li class="related_box"  >
    		<div class="r_pic">
    		<a href="" title="" target="_blank">
    		<img src="" alt="" class="thumbnail" />
    		a>
    		div>
    		<div class="r_title"><a href="" title="" target="_blank" rel="bookmark"> the_title(); ?>a>div>
    		li>
    	
    		$exclude_id .= ',' . $post->ID; $i ++;
    	} wp_reset_query();
    }
    if ( $i < $post_num ) {
    	$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
    	$args = array(
    		'category__in' => explode(',', $cats),
    		'post__not_in' => explode(',', $exclude_id),
    		'caller_get_posts' => 1,
    		'orderby' => 'comment_date',
    		'posts_per_page' => $post_num - $i
    	);
    	query_posts($args);
    	while( have_posts() ) { the_post(); ?>
    	<li class="related_box"  >
    		<div class="r_pic">
    		<a href="" title="" target="_blank">
    		<img src="" alt="" class="thumbnail" />
    		a>
    		div>
    		<div class="r_title"><a href="" title="" target="_blank" rel="bookmark"> the_title(); ?>a>div>
    	li>
    	 $i++;
    	} wp_reset_query();
    }
    if ( $i  == 0 )  echo '
    沒有相關(guān)文章!
    ';
    ?>
    ul>
    

    PS:第四行$post_num = 4; 表示調(diào)用4篇文章,請根據(jù)自己需要修改。css樣式自己寫,也可參考一下:

    .related_posts{margin-top:5px;}
    .related_img{width:600px;height:210px;}
    .related_box{float:left;overflow:hidden;margin-top:5px;width:148px;border-right:1px #eee solid}
    .related_box:hover{background:#f9f9f9}
    .related_box .r_title{width:auto;height:72px;font-weight:400;font-size:14px;margin:0 10px;overflow:hidden;}
    .related_box .r_pic{margin:6px}
    .related_box .r_pic img{width:130px;height:100px;border:1px  solid #e1e1e1;background:#fff;padding:2px}
    

    二、五種不同添加(僅標題)方法

    1、標簽相關(guān)

    首先獲取文章的所有標簽,接著獲取這些標簽下的 n 篇文章,那么這 n 篇文章就是與該文章相關(guān)的文章了。現(xiàn)在可以見到的WordPress相關(guān)文章插件都是使用的這個方法。下面是實現(xiàn)的代碼:

    "tags_related">
    
    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) ];
    
      // 該方法使用 query_posts() 函數(shù)來調(diào)用相關(guān)文章,以下是參數(shù)列表
      $args = array(
            'tag__in' => array($post_tag),
            'category__not_in' => array(NULL),  // 不包括的分類ID'post__not_in' => array($post->ID),
            'showposts' => 6,                           // 顯示相關(guān)文章數(shù)量'caller_get_posts' => 1
        );
      query_posts($args);
    
      if (have_posts()) {
        while (have_posts()) {
          the_post(); update_post_caches($posts); ?><li>* <a href="" rel="bookmark" title=""> the_title(); ?>a>li>
    
        }
      }
      else {
        echo '
    * 暫無相關(guān)文章
    ';
      }
      wp_reset_query(); 
    }
    else {
      echo '
    * 暫無相關(guān)文章
    ';
    }
    ?>
    ul>
    

    使用說明:"不包括的分類ID" 指的是相關(guān)文章不顯示該分類下的文章,將同行的 NULL 改成文章分類的ID即可,多個ID就用半角逗號隔開。因為這里限制只顯示6篇相關(guān)文章,所以不管給 query_posts() 的參數(shù) tag__in 賦多少個值,都是只顯示一個標簽下的 6 篇文章,除非第一個標簽有1篇,第二個標簽有2篇,第三個有3篇。。。。。。所以如果這篇文章有多個標簽,那么我們采取的做法是隨機獲取一個標簽的id,賦值給 tag__in 這個參數(shù),獲取該標簽下的6篇文章。

    2、分類相關(guān)

    本方法是通過獲取該文章的分類id,然后獲取該分類下的文章,來達到獲取相關(guān)文章的目的。

    "cat_related">
    
    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="" rel="bookmark" title=""> the_title(); ?>a>li>
    
        }
      } 
      else {
        echo '
    * 暫無相關(guān)文章
    ';
      }
      wp_reset_query(); 
    }
    else {
      echo '
    * 暫無相關(guān)文章
    ';
    }
    ?>
    ul>
    
    方法三:標簽相關(guān),SQL獲取

    獲取相關(guān)文章的原理與方法一相似,不過在獲取文章的時候是以SQL語句來直接讀取數(shù)據(jù)庫,從而隨機獲取6篇相關(guān)文章記錄,而不是WordPress的函數(shù)query_posts()。

    "tags_related">
    
    global $post, $wpdb;
    $post_tags = wp_get_post_tags($post->ID);
    if ($post_tags) {
        $tag_list = '';
        foreach ($post_tags as $tag) {
            // 獲取標簽列表
            $tag_list .= $tag->term_id.',';
        }
        $tag_list = substr($tag_list, 0, strlen($tag_list)-1);
    
        $related_posts = $wpdb->get_results("
            SELECT DISTINCT ID, post_title
            FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
            WHERE {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
            AND ID = object_id
            AND taxonomy = 'post_tag'
            AND post_status = 'publish'
            AND post_type = 'post'
            AND term_id IN (" . $tag_list . ")
            AND ID != '" . $post->ID . "'
            ORDER BY RAND()
            LIMIT 6");
            // 以上代碼中的 6 為限制只獲取6篇相關(guān)文章// 通過修改數(shù)字 6,可修改你想要的文章數(shù)量
    
        if ( $related_posts ) {
            foreach ($related_posts as $related_post) {
    ?><li><a href="ID); ?>" rel="bookmark" title="post_title; ?>"> echo $related_post->post_title; ?>a>li>
       } 
        }
        else {
          echo '
    暫無相關(guān)文章
    ';
        } 
    }
    else {
      echo '
    暫無相關(guān)文章
    ';
    }
    ?>
    ul>
    
    方法四:分類相關(guān),SQL獲取

    獲取相關(guān)文章的原理與方法二相似,不過在獲取文章的時候是以SQL語句來直接讀取數(shù)據(jù)庫,從而隨機獲取6篇相關(guān)文章記錄,而不是WordPress的函數(shù)query_posts()。

    "cat_related">
    
    global $post, $wpdb;
    $cats = wp_get_post_categories($post->ID);
    if ($cats) {
      $related = $wpdb->get_results("
      SELECT post_title, ID
      FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
      WHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id
      AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category'
      AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
      AND {$wpdb->prefix}posts.post_status = 'publish'
      AND {$wpdb->prefix}posts.post_type = 'post'
      AND {$wpdb->prefix}term_taxonomy.term_id = '" . $cats[0] . "'
      AND {$wpdb->prefix}posts.ID != '" . $post->ID . "'
      ORDER BY RAND( )
      LIMIT 6");
    
      if ( $related ) {
          foreach ($related as $related_post) {
    ?><li>* <a href="ID); ?>" rel="bookmark" title="post_title; ?>"> echo $related_post->post_title; ?>a>li>
    
        } 
      }
      else {
        echo '
    * 暫無相關(guān)文章
    ';
      } 
    }
    else {
      echo '
    * 暫無相關(guān)文章
    ';
    }
    ?>
    ul>
    
    方法五:作者相關(guān)

    該方法是獲取該文章作者的其他文章來充當相關(guān)文章,代碼如下:

    "author_related">
    global $post;
      $post_author = get_the_author_meta( 'user_login' );
      $args = array(
            'author_name' => $post_author,
            'post__not_in' => array($post->ID),
            'showposts' => 6,               // 顯示相關(guān)文章數(shù)量'orderby' => date,          // 按時間排序'caller_get_posts' => 1
        );
      query_posts($args);
    
      if (have_posts()) {
        while (have_posts()) {
          the_post(); update_post_caches($posts); ?><li>* <a href="" rel="bookmark" title=""> the_title(); ?>a>li>
    
        }
      }
      else { 
        echo '
    * 暫無相關(guān)文章
    ';
      }
      wp_reset_query();
    ?>
    ul>
    

    CSS 樣式優(yōu)先級

    2019-5-16 1:06:25

    國內(nèi)主題

    Return - 適合個人博客、自媒體的Wordpress主題

    2020-2-2 3:02:00

    ??
    Npcink上的部份代碼及教程來源于互聯(lián)網(wǎng),僅供網(wǎng)友學習交流,若您喜歡本文可附上原文鏈接隨意轉(zhuǎn)載。
    無意侵害您的權(quán)益,請發(fā)送郵件至 1355471563#qq.com 或點擊右側(cè) 私信:Muze 反饋,我們將盡快處理。
    0 條回復 A文章作者 M管理員
      暫無討論,說說你的看法吧
    ?
    個人中心
    購物車
    優(yōu)惠劵
    今日簽到
    有新私信 私信列表
    搜索
    主站蜘蛛池模板: 波多野结衣一区二区三区| 中文字幕一区日韩在线视频 | 亚洲不卡av不卡一区二区| 男人的天堂av亚洲一区2区 | 波多野结衣在线观看一区二区三区| 国精无码欧精品亚洲一区| 一区二区精品在线观看| 欧美日韩综合一区二区三区| 精品亚洲A∨无码一区二区三区 | 精品国产一区二区三区AV性色 | 亚洲A∨无码一区二区三区 | а天堂中文最新一区二区三区| 中文字幕在线不卡一区二区| 91秒拍国产福利一区| 少妇激情AV一区二区三区| 中文字幕av无码一区二区三区电影| 国产91精品一区二区麻豆亚洲| 无码AV动漫精品一区二区免费| 精品一区二区三区无码视频| 国产精品亚洲综合一区在线观看 | 久久久久人妻一区二区三区| 老熟妇仑乱视频一区二区 | 国产精品香蕉在线一区| 无码人妻精品一区二区三区蜜桃 | 中文字幕一区二区日产乱码| 亚洲一区二区三区四区在线观看| 又硬又粗又大一区二区三区视频| 99久久精品国产高清一区二区| 日韩电影在线观看第一区| 一区二区网站在线观看| A国产一区二区免费入口| 国产精品无圣光一区二区| 日韩免费观看一区| 亚欧在线精品免费观看一区| 无码中文字幕人妻在线一区二区三区| 国模无码一区二区三区| 精品一区二区三区四区电影| 国产福利电影一区二区三区,日韩伦理电影在线福 | 精品一区二区三区在线成人| 国产91久久精品一区二区| 大香伊蕉日本一区二区|