導讀:建站技術建站技術之前大叔介紹過 wordpress 相關文章實現的方法,例:《代碼實現WordPress相關文章》,那么今天說的這個教程,是從優化角度來更合理的實現wordpr公司網站搭建網站搭建教程。

之前模板下載吧介紹過 wordpress相關文章實現的方法,例:《代碼實現WordPress相關文章》,那么今天說的這個教程,是從優化角度來更合理的實現wordpress相關文章的,至于客觀喜歡哪個,自己決定吧!
策略:文章內容相關程度: 手動指定 > 標簽 >分類 > 隨機

實現方式:下面代碼直接加到functions.php中即可
function?add_related_posts($content){
????return?$content?.?wp_related_posts();
}
add_filter?('the_content',?'add_related_posts');?//hook
function?wp_related_posts(){
????global?$post;
????$num?=?5;//文章數量
????$counter?=?1;
????$exclude_id?=?get_post_meta($post->ID,'related',true);//獲取手動置頂的相關文章
????if?($exclude_id){
????????$args?=?array(
????????????'post_status'?=>?'publish',
????????????'post_type'?=>織夢首頁模板?array('post'),
????????????'post__in'?=>?explode(',',?$exclude_id),
????????????'posts_per_page'?=>?$num
????????);
????????$posts?=?get_posts($args);
????????foreach($posts?as?$sb){
????????????$output?.=?'<li><a?href="'?.?get_permalink($sb->ID)?.?'">'?.?$sb->post_title?.?'</a></li>';//可自定義樣式
????????????$i++;
????????}
????}
????if(?$i?<?$num){//自定義文章數不足后通過分類和標簽處理
????????$tagsid?=?array();
????????$catid?=?array();
????????$thisid[]?=?$post->ID;
????????$posttags?=?get_the_tags();
????????$catids?=?get_the_category();
????????if(!emptyempty($posttags))?{
????????????foreach($posttags?as?$tag)?{
關鍵詞標簽: 建站 相關文章 自定義
聲明: 本文由我的SEOUC技術文章主頁發布于:2023-07-23 ,文章wordpress相關文章的優化機智和自定義設置,建站技主要講述相關文章,自定義,建站網站建設源碼以及服務器配置搭建相關技術文章。轉載請保留鏈接: http://www.bifwcx.com/article/web_35368.html