方法三:標(biāo)簽相關(guān),SQL獲取
獲取相關(guān)文章的原理與方法一相似,不過在獲取文章的時候是以SQL語句來直接讀取數(shù)據(jù)庫,從而隨機(jī)獲取6篇相關(guān)文章記錄,而不是WordPress的函數(shù)query_posts().
<ul id="tags_related"> <?php $post_tags = wp_get_post_tags($post->ID); if ($post_tags) { foreach ($post_tags as $tag) { // 獲取標(biāo)簽列表 $tag_list[] .= $tag->term_id; } // 隨機(jī)獲取標(biāo)簽列表中的一個標(biāo)簽 $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ]; $related = $wpdb->get_results(" SELECT {$wpdb->prefix}posts.post_title, {$wpdb->prefix}posts.guid 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 = 'post_tag' 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 = '" . $post_tag . "' AND {$wpdb->prefix}posts.ID != '" . $post->ID . "' ORDER BY RAND( ) LIMIT 6"); // 以上代碼中的 6 為限制只獲取6篇相關(guān)文章 // 通過修改數(shù)字 6,可修改你想要的文章數(shù)量 if ( $related ) { foreach ($related as $related_post) { ?> <li>* <a href="<?php echo $related_post->guid; ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li> <?php } } else { ?> <li>* 暫無相關(guān)文章</li> <?php } }?> </ul>
方法四:分類相關(guān),SQL獲取
獲取相關(guān)文章的原理與方法二相似,不過在獲取文章的時候是以SQL語句來直接讀取數(shù)據(jù)庫,從而隨機(jī)獲取6篇相關(guān)文章記錄,而不是WordPress的函數(shù)query_posts().
<ul id="cat_related"> <?php $cats = wp_get_post_categories($post->ID); if ($cats) { $cat = get_category( $cats[0] ); $first_cat = $cat->cat_ID; $related = $wpdb->get_results(" SELECT wp_posts.post_title, wp_posts.guid FROM wp_posts, wp_term_relationships, wp_term_taxonomy WHERE wp_posts.ID = wp_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 = '" . $first_cat . "' AND {$wpdb->prefix}posts.ID != '" . $post->ID . "' ORDER BY RAND( ) LIMIT 6"); if ( $related ) { foreach ($related as $related_post) { ?> <li>* <a href="<?php echo $related_post->guid; ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li> <?php } } else { ?> <li>* 暫無相關(guān)文章</li> <?php } }?> </ul>
方法五:作者相關(guān)
該方法是獲取該文章作者的其他文章來充當(dāng)相關(guān)文章,代碼如下:
<ul id="author_related"> <?php $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="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endwhile; else : ?> <li>* 暫無相關(guān)文章</li> <?php endif; wp_reset_query(); ?> </ul>
時間效率對比
我們將用之前的一個php代碼對以上各個相關(guān)文章代碼執(zhí)行時間進(jìn)行測算,以便對以上各個的方法進(jìn)行效率,給你的選擇提供參考。以下是在同一篇文章中獲取6篇相關(guān)文章,以上各方法最終測算的時間如下:
方法一:0.18067908287048 秒
方法二:0.057158946990967 秒
方法三:0.037126064300537 秒
方法四:0.045628070831299 秒
方法五:0.023991823196411 秒
原文:http://www.ludou.org/how-to-generate-related-posts-in-wordpress.html
本文鏈接:http://m.95time.cn/tech/program/2010/7945.asp
出處:露兜博客
責(zé)任編輯:bluehearts
上一頁 WordPress代碼實(shí)現(xiàn)相關(guān)文章的幾種方法 [1] 下一頁
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|