不用插件在wordpress中实现(最新评论/最热文章/相关文章/最新文章/随机文章)
发表于七月 1, 2009 栏目:建站分享 本文有 845 位读者进行研读
wordpress用的人越来越多,本站就是用wp搭建的。
在wp中许多功能并没有集成,如果需要某些适用的功能,还需要加装插件之类。不过有的插件装起来麻烦,影响速度。不如直接用代码写。
如下面将要介绍的最新评论,最热文章,相关文章,最新文章,随机文章五个常用功能。
最新评论:
在需要添加最新评论的地方插入如下代码则可:
<?php
global $wpdb;
$sql= “SELECT DISTINCT ID, post_title, post_password, comment_ID,comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_type,comment_author_url, SUBSTRING(comment_content,1,30) AScom_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHEREcomment_approved = ‘1′ AND comment_type = ” AND post_password = ” ORDERBY comment_date_gmt DESC LIMIT 10″;
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$output.= “\n<li>”. “<a href=\”" .get_permalink($comment->ID).”#comment-” . $comment->comment_ID .”\” title=\”on “.$comment->post_title .”\”>”.strip_tags($comment->comment_author).”</a>” .”: “.strip_tags($comment->com_excerpt).”</li>”;
}
$output .= $post_HTML;
echo $output;
?>
最热文章:
在需要添加评论最多的文章列表地方插入如下代码则可:
<ul>
<?php$result = $wpdb->get_results(“SELECT comment_count,ID,post_titleFROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10″);
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=”<?php echo $title ?>”>
<?php echo $title ?></a> (<?php echo $commentcount ?>)</li>
<?php } } ?>
</ul>
相关文章:
是的,就是连相关文章列表我们也不需要插件支持,下面的代码会根据文章中的tag标签自动判断何篇文章与当前相关,而且相关性也很强!
<ul>
<?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(); ?>
<li><ahref=”<?php the_permalink() ?>” rel=”bookmark” title=”PermanentLink to <?php the_title_attribute(); ?>”><?php the_title();?> <?php comments_number(‘ ‘,’(1)’,'(%)’); ?></a></li>
<?php
endwhile;
}
}
?>
</ul>
最新文章:
调用代码如下:
<?php
$limit = get_option(‘posts_per_page’);
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
query_posts(’showposts=’ . $limit=7 . ‘&paged=’ . $paged);
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
<?php while(have_posts()) : the_post(); if(!($first_post == $post->ID)) : ?>
<ul>
<li><ahref=”<?php the_permalink() ?>” rel=”bookmark” title=”PermanentLink to <?php the_title_attribute(); ?>”><?php the_title();?></a></li>
</ul>
<?php endif; endwhile; ?>
随机文章:
<?php
query_posts(array(‘orderby’ => ‘rand’, ’showposts’ => 1));
if (have_posts()) :
while (have_posts()) : the_post();
the_title();
the_excerpt();
endwhile;
endif; ?>
阅读本文的读者还阅读了以下文章:
您也可以像以下达人一样发表观点: (3)
这个适合有编程基础的人。
复制进去试试。。。
试了前两个,都失败。有错误。