人妻精品在线观看一区二区三区,蜜臀av精品一区二区三区网站,中文一区二区三区亚洲欧美,熟女人妇精品一区二区,人妻av在线观看视频,欧美日韩国产三级精品网站,黄色免费网站直接进入,超碰公开福利正在播放,国产毛片乡下农村妇女毛片

WordPress怎么實(shí)現(xiàn)相關(guān)文章功能?幾種方法分享

來源:php中文網(wǎng) | 2023-03-01 21:04:02 |

WordPress怎么實(shí)現(xiàn)相關(guān)文章功能?下面本篇文章給大家介紹一下WordPress代碼實(shí)現(xiàn)相關(guān)文章的幾種方法,希望對(duì)大家有所幫助!

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


(相關(guān)資料圖)

這里我整理編寫了幾種用代碼實(shí)現(xiàn)相關(guān)文章的方法,這其中會(huì)詳細(xì)標(biāo)明各部分代碼的作用,以及如何自定義你想要的功能,希望對(duì)大家有所幫助。開始之前,說明一點(diǎn),以下所有方法輸出的HTML代碼格式都是以下形式,你可以根據(jù)需要進(jìn)行修改:

<ul id="xxx">    <li>* <a title="文章標(biāo)題1" rel="bookmark" href="文章鏈接1">文章標(biāo)題1</a></li>    <li>* <a title="文章標(biāo)題2" rel="bookmark" href="文章鏈接2">文章標(biāo)題2</a></li>    ......</ul>

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

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

<ul id="tags_related"><?phpglobal $post;$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)簽列表中的一個(gè)標(biāo)簽  $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    }  }  else {    echo "<li>* 暫無相關(guān)文章</li>";  }  wp_reset_query(); }else {  echo "<li>* 暫無相關(guān)文章</li>";}?></ul>

使用說明:"不包括的分類ID" 指的是相關(guān)文章不顯示該分類下的文章,將同行的 NULL 改成文章分類的ID即可,多個(gè)ID就用半角逗號(hào)隔開。因?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篇。。。。。。所以如果這篇文章有多個(gè)標(biāo)簽,那么我們采取的做法是隨機(jī)獲取一個(gè)標(biāo)簽的id,賦值給 tag__in 這個(gè)參數(shù),獲取該標(biāo)簽下的6篇文章。

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

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

<ul id="cat_related"><?phpglobal $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="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li><?php    }  }   else {    echo "<li>* 暫無相關(guān)文章</li>";  }  wp_reset_query(); }else {  echo "<li>* 暫無相關(guān)文章</li>";}?></ul>

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

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

<ul id="tags_related"><?phpglobal $post, $wpdb;$post_tags = wp_get_post_tags($post->ID);if ($post_tags) {    $tag_list = "";    foreach ($post_tags as $tag) {        // 獲取標(biāo)簽列表        $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="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li><?php   }     }    else {      echo "<li>暫無相關(guān)文章</li>";    } }else {  echo "<li>暫無相關(guān)文章</li>";}?></ul>

方法四:分類相關(guān),SQL獲取

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

<ul id="cat_related"><?phpglobal $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="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li><?php    }   }  else {    echo "<li>* 暫無相關(guān)文章</li>";  } }else {  echo "<li>* 暫無相關(guān)文章</li>";}?></ul>

方法五:作者相關(guān)

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

<ul id="author_related"><?php  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,// 按時(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    }  }  else {     echo "<li>* 暫無相關(guān)文章</li>";  }  wp_reset_query();?></ul>

時(shí)間效率對(duì)比

我們對(duì)以上各個(gè)相關(guān)文章代碼執(zhí)行時(shí)間進(jìn)行測(cè)算,以便對(duì)以上各個(gè)的方法進(jìn)行效率,給你的選擇提供參考。以下是在同一篇文章中獲取6篇相關(guān)文章,以上各方法最終測(cè)算的時(shí)間如下:

方法一:0.18067908287048 秒 方法二:0.057158946990967 秒 方法三:0.037126064300537 秒 方法四:0.045628070831299 秒 方法五:0.023991823196411 秒

推薦學(xué)習(xí):《WordPress教程》

以上就是WordPress怎么實(shí)現(xiàn)相關(guān)文章功能?幾種方法分享的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!

關(guān)鍵詞: WordPress