18

WordPress主题中不使用插件就能实现的五个常用功能

这篇文章是从Jinwen Say那边看来的,他是一个功力高深的wordpress blogger,提供了许多wordpress模板,还有jQuery的应用,有兴趣的朋友也可以过去他那边挖宝喔:)

文章里头提到wordpress本身可以支持五种文章延伸的代码,包含最新文章,最新回响,热门文章,相关文章以及随机文章。通常这五种功能我们都会使用插件,但是装太多插件对于blog的读取速度也会降低,所以插件能省则省。

当我们在寻找一个合适的WordPress主题时,对主题所包含的基本功能有所要求的同时又希望主题不用强制使用某些插件。总结在WordPress主题的日常使用中,其实我们常用的一些功能,它们并不需要依靠插件才能实现的。譬如下面将要介绍的最新评论最热文章相关文章最新文章随机文章五个常用功能。

最新评论:

在需要添加最新评论的地方插入如下代码则可:

<?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) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = ’1′ AND comment_type = ” AND post_password = ” ORDER BY 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;
?>

补充,如果你在调用上面代码中出现”syntax error”的报错,可以看下这篇【修正文章】,thanks muki 同学。

最热文章:

在需要添加评论最多的文章列表地方插入如下代码则可:

<ul”most-comments”>
<?php $result = $wpdb->get_results(“SELECT comment_count,ID,post_title FROM $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><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link 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><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link 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; ?>

上面的5个功能是一般WordPress主题中被使用得最为频繁,而且在一个WordPress主题中内建这些功能其实很容易。另外,使用这些简单的代码不仅让主题需要使用的插件得以减少,或者这还是那些正在寻找合适主题的朋友的愿望所在。

原文链接: http://blog.mukispace.com/2009/04/16/wordpress-five-posts-code-without-plugins/

当前没有评论!

第一个在本文留言。

发表评论

名字(必须)
邮箱(必须),(永不被公布)
网址(建议)

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!:

字体为 粗体 是必填项目,邮箱地址 永远不会 公布。

允许部分 HTML 代码:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs(网站链接)必须完整有效 (比如: http://www.tan118.cn),所有标签都必须完整的关闭。

超出部分系统将会自动分段及换行。

请保证评论内容是与日志或 Blog 内容相关的,灌水、攻击性或不恰当的评论 可能 会被编辑或删除。

    搜索

    标签云

    宣传栏