wordpress實現(xiàn)相關(guān)文章功能代碼方法
來源:易賢網(wǎng) 閱讀:964 次 日期:2016-07-02 08:50:03
溫馨提示:易賢網(wǎng)小編為您整理了“wordpress實現(xiàn)相關(guān)文章功能代碼方法”,方便廣大網(wǎng)友查閱!

wordpress有很多實現(xiàn)相關(guān)文章功能的插件,插件的優(yōu)點是配置簡單,但是可能會對網(wǎng)站的速度造成一些小的影響,所以很多人還是比較喜歡用代碼實現(xiàn)需要的功能,但是話又說回來了,代碼實現(xiàn)也有缺點,就是配置復雜,不懂代碼的人完全摸不著頭腦或者只能照搬別人的代碼,還不如用插件。

這里我整理編寫了幾種用代碼實現(xiàn)相關(guān)文章的方法,這其中會詳細標明各部分代碼的作用,以及如何自定義你想要的功能,希望對大家有所幫助,有什么問題可以給本文發(fā)表評論,我會及時給你回復。開始之前,說明一點,以下所有方法輸出的html代碼格式都是以下形式,你可以根據(jù)需要進行修改:

<ul id=xxx>

<li>* <a title=文章標題1 rel=bookmark href=文章鏈接1>文章標題1</a></li>

<li>* <a title=文章標題2 rel=bookmark href=文章鏈接2>文章標題2</a></li>

......

</ul>

方法一:標簽相關(guān)

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

<ul id=tags_related>

<?php

$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=<?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>

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

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

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

<ul id=cat_related>

<?php

$cats = wp_get_post_categories($post->id);

if ($cats) {

$cat = get_category( $cats[0] );

$first_cat = $cat->cat_id;

$args = array(

'category__in' => array($first_cat),

'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 endwhile; else : ?>

<li>* 暫無相關(guān)文章</li>

<?php endif; wp_reset_query(); } ?>

</ul>

方法三:標簽相關(guān),sql獲取

獲取相關(guān)文章的原理與方法一相似,不過在獲取文章的時候是以sql語句來直接讀取數(shù)據(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)

{

// 獲取標簽列表

$tag_list[] .= $tag->term_id;

}

// 隨機獲取標簽列表中的一個標簽

$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ù)庫,從而隨機獲取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)

該方法是獲取該文章作者的其他文章來充當相關(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í)行時間進行測算,以便對以上各個的方法進行效率,給你的選擇提供參考。以下是在同一篇文章中獲取6篇相關(guān)文章,以上各方法最終測算的時間如下:

方法一:0.18067908287048 秒

方法二:0.057158946990967 秒

方法三:0.037126064300537 秒

方法四:0.045628070831299 秒

方法五:0.023991823196411 秒

更多信息請查看CMS教程
易賢網(wǎng)手機網(wǎng)站地址:wordpress實現(xiàn)相關(guān)文章功能代碼方法
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇剩?/div>
相關(guān)閱讀CMS教程
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)