Wordpress的强大和易用,在于无数的主题和插件,但插件多了会影响网站的速度和安全。如果能用简单代码实现需要的功能,还是用代码好了。 在每篇文章的末尾,显示相关文章,可以更好的留住客户。 下面提供两种方法:
第一种
在所用主题的functions.php文件中,添加如下代码
function wp_get_related_posts()
{
global $wpdb, $post,$table_prefix;
$limit = 10; //How Many Related Posts Displayed
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 $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 $related_posts = $wpdb->get_results($q);
$output = “”;
if (!$related_posts)
{
$output .= ‘
}
foreach ($related_posts as $related_post )
{
$dateformat = get_option(‘date_format’);
$output .= ‘
$output .= ‘ID).'” title=”‘.wptexturize($related_post->post_title).’ (‘.mysql2date($dateformat, $related_post->post_date).’)”>’.wptexturize($related_post->post_title).' (‘.$related_post->comment_count .’)’;
$output .= ‘
}
$output = ‘
Related Posts
- ’ . $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);
第二种
在所用主题的single.php添加,一般添加在之后
Related Posts
- ”>
爱吧直接使用的第二种方法。
推荐站内搜索:韩国服务器推荐、免费云服务器、免费域名空间、查询域名是否备案海外虚拟主机空间、游戏高防服务器租用、备案查询 工信部、备案系统、免费的网站服务器、国内php空间、