日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 中 相关文章 的思路,WordPress实现推荐相关文章功能代码

發布時間:2023/12/15 php 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 中 相关文章 的思路,WordPress实现推荐相关文章功能代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

WordPress實現推薦相關文章功能有2種方法:一種是可以在單篇日志和 feed 中都生成推薦相關文章功能,不過,功能越強大,代碼也就會相應較多,所以這里還提供第二種,僅在單篇日志中實現在相關日志的方法。

方法一:單篇日志和 feed 中都可以生成相關日志

把以下代碼復制到 WordPress 的主題文件?functions.php 中:

function wp_get_related_posts()

{

global $wpdb, $post,$table_prefix;

$limit = 10; //顯示幾條相關文章

if(!$post->ID){return;}

$now = current_time('mysql', 1);

$tags = wp_get_post_tags($post->ID);

$taglist = "'" . $tags[0]->term_id. "'";

$tagcount = count($tags);

if ($tagcount > 1) {

for ($i = 1; $i < $tagcount; $i++) {

$taglist = $taglist . ", '" . $tags[$i]->term_id . "'";

}

}

$limitclause = "LIMIT $limit";

$q = "SELECT p.ID, p.post_title, p.post_date, p.comment_count, count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE t_t.taxonomy ='post_tag' AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id = p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt < '$now' GROUP BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC $limitclause;";

$related_posts = $wpdb->get_results($q);

$output = "";

if (!$related_posts)

{

$output .= '

無相關日志';

}

foreach ($related_posts as $related_post )

{

$dateformat = get_option('date_format');

$output .= '

';

$output .= ''.wptexturize($related_post->post_title).' ('. $related_post->comment_count .')';

$output .= '

';

}

$output = '

相關日志

  • ' . $output . '
';

return $output;

}

function wp_related_posts_attach($content)

{

if (is_single()||is_feed())

{

$output = wp_get_related_posts();

$content = $content . $output;

}

return $content;

}

add_filter('the_content', 'wp_related_posts_attach',100);

方法二:僅在單篇日志中顯示相關日志

在 WordPress 主題文件?single.php 中需要的位置插入以下代碼即可:

相關日志

$tags = wp_get_post_tags($post->ID);

if ($tags) {

$first_tag = $tags[0]->term_id;

$args=array(

'tag__in' => array($first_tag),

'post__not_in' => array($post->ID),

'showposts'=>10,

'caller_get_posts'=>1

);

$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {

while ($my_query->have_posts()) : $my_query->the_post(); ?>

<?php the_title();?> <?php comments_number(' ','(1)','(%)'); ?>

endwhile;

}

}

wp_reset_query();

?>

1.添加標題列表樣式的相關文章

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

相關文章

$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(); ?>

<?php the_title(); ?>

$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(); ?>

<?php the_title(); ?>

} wp_reset_query();

}

if ( $i == 0 ) echo '

沒有相關文章!';

?>

PS:第四行$post_num = 8;表示顯示8篇文章,請根據自己的需要修改。

顯示樣式需要自己寫css,可以參考一下下面的:

.related_posts{margin-top:5px;}

.related_posts li{margin-left:20px;color:#444;list-style:circle;font-size:14px;line-height:26px;padding:0 0 0 5px}

2.添加含縮略圖的相關文章

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張圖片,放在現用主題文件夾下的 images/pic/ 目錄,圖片為jpg格式,并且使用數字 1-10命名,比如 1.jpg;如果你不想用隨機圖片,請將?倒數第5行?前面的“//”去掉,然后給?倒數第7、9行?前面添加“//”注銷,并且在現用主題的 /images/ 目錄下添加一張名字為 default_thumb.jpg 的默認圖片,這樣,就會顯示默認圖片。

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

相關文章

$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(); ?>

<?php the_title(); ?>

$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(); ?>

<?php the_title(); ?>

} wp_reset_query();

}

if ( $i == 0 ) echo '

沒有相關文章!';

?>

PS:第四行$post_num = 4; 表示調用4篇文章,請根據自己需要修改。

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}

注:代碼參考自cmhello的hcms主題。

總結

以上是生活随笔為你收集整理的php 中 相关文章 的思路,WordPress实现推荐相关文章功能代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。