非插件 短代码实现最新评论 最热文章 相关文章 标签云调用
WordPress的插件的确很丰富,使用起来也很方便,但部分插件间有可能存在冲突,部分插件如不再使用会留下些后遗症,插件用多了还会增加服务器压力,我是尽量不用插件的,博客上很多功能基本上是在function.php里添加函数实现短代码调用或用JS实现的。这里要讲的是怎样用function.php短代码调用最新评论、最热文章、相关文章、标签云。
最新评论代码:WordPress默认的是以(评论者on文章名)形式的,觉得这样不是很好,这里将其修改为(评论者:评论内容)形式,并把链接改为评论链接。用
<ul>
<?php dp_recent_comments(); ?>
</ul>
调用。并将以下代码加入function.php中(在最后一个“?>”前就可以):
/* RECENT COMMENTS
—————————
*/
function dp_recent_comments($no_comments = 5, $comment_len = 65) {
global $wpdb;
$request = "SELECT * FROM $wpdb->comments";
$request .= " JOIN $wpdb->posts ON ID = comment_post_ID";
$request .= " WHERE comment_approved = ‘1′ AND post_status = ‘publish’ AND post_password =”";
$request .= " ORDER BY comment_date DESC LIMIT $no_comments";
$comments = $wpdb->get_results($request);
if ($comments) {
foreach ($comments as $comment) {
ob_start();
?>
<li>
<a href="<?php echo get_permalink( $comment->comment_post_ID ) . ‘#comment-’ . $comment->comment_ID; ?>"><?php echo dp_get_author($comment); ?>: <span style="color:#333; "></a><?php echo strip_tags(substr(apply_filters(‘get_comment_text’, $comment->comment_content), 0, $comment_len)); ?></span> …
</li>
<?php
ob_end_flush();
}
} else {
echo ‘<li>’.__(‘No comments’, ‘banago’).”;
}
}
function dp_get_author($comment) {
$author = "";
if ( empty($comment->comment_author) )
$author = __(‘Anonymous’, ‘banago’);
else
$author = $comment->comment_author;
return $author;
}
还可根据需要加入调用头像代码。
最热文章代码:调用方式为
<ul>
<?php wp_popular_posts(); ?>
</ul>
将以下代码加入function.php中(在最后一个“?>”前就可以)
/* POPULAR POSTS
—————————
*/
function wp_popular_posts() {
global $wpdb;
$result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) {
?>
<li><a href="<?php echo get_permalink($postid); ?>" title="Permalink to <?php echo $title; ?>"><?php echo $title; ?></a></li>
<?php
}
}
}
相关文章代码:调用方式为
<ul>
<?php wp_popular_posts(); ?>
</ul>
将以下代码加入function.php中(在最后一个“?>”前就可以):
/* RELATED POSTS
—————————
*/
function wp_related_posts($postid) {
$tags = wp_get_post_tags($postid);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
‘tag__in’ => $tag_ids,
‘post__not_in’ => array($post->ID),
’showposts’=>5, // Number of related posts that will be shown.
‘caller_get_posts’=>1
);
$related_post = new wp_query($args);
if( $related_post->have_posts() ) {
while ($related_post->have_posts()) {
$related_post->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
}
}
}
标签云代码:可以调用系统的代码:<?php wp_tag_cloud(); ?>
原创文章如转载,请注明:转载自 可口鱼 [ http://www.kekouyu.com ]
本文链接地址:http://www.kekouyu.com/archives/100.html

类归于:
标签:





















1 RESPONSES TO 非插件 短代码实现最新评论 最热文章 相关文章 标签云调用
受教 拿走到自己博客去试了 现在一想到插件太多 我头就大