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

2958aa523b8e4e9e9dc07d7fa092534b.png

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主题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在 WordPress 添加网站公告功能,可以使用自定义文章类型来实现。以下是一段 PHP 代码,用于创建一个自定义文章类型 'notice': ```php function create_post_type() { register_post_type( 'notice', array( 'labels' => array( 'name' => __( '公告' ), 'singular_name' => __( '公告' ) ), 'public' => true, 'has_archive' => true, 'rewrite' => array('slug' => 'notice'), ) ); } add_action( 'init', 'create_post_type' ); ``` 接下来,可以在 WordPress 后台添加一个新的“公告”文章类型。在这个自定义文章类型,可以添加公告的标题、内容、发布日期、到期日期等信息。在显示公告时,可以根据发布日期和到期日期来判断是否需要显示该公告。 以下是一个简单的公告显示代码,用于在页面上显示最新的一条公告: ```php $args = array( 'post_type' => 'notice', 'posts_per_page' => 1, 'orderby' => 'post_date', 'order' => 'DESC', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'notice_start_date', 'value' => date('Ymd'), 'compare' => '<=', 'type' => 'DATE' ), array( 'key' => 'notice_end_date', 'value' => date('Ymd'), 'compare' => '>=', 'type' => 'DATE' ) ) ); $notices = new WP_Query($args); if ($notices->have_posts()) { while ($notices->have_posts()) { $notices->the_post(); ?> <div class="notice"> <h3><?php the_title(); ?></h3> <?php the_content(); ?> </div> <?php } } wp_reset_postdata(); ``` 在这段代码,使用 WP_Query 类来获取最新的一条公告,并根据发布日期和到期日期来判断是否需要显示该公告。如果找到了一条公告,就在页面上显示该公告的标题和内容。 这只是一个简单的实现方式,你可以根据自己的需求来进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值