WordPress 主题无插件实现五个常用功能
Jinwen 发布了一篇: WordPress 主题中不使用插件就能实现的五个常用功能, 大家都知道使用较少插件, 会明显的加速 WordPress 速度, 当我们在寻找一个合适的 WordPress 主题时, 对主题所包含的基本功能有所要求的同时又希望主题不用强制使用某些插件. 总结在 WordPress 主题的日常使用中, 其实我们常用的一些功能,它们并不需要依靠插件才能实现的. 譬如下面将要介绍的 最新文章,最热文章,最新评论,相关文章,随机文章 五个常用功能.
最新文章:
调用代码如下:
1 2 3 4 5 6 7 8 9 10 11 | <?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; ?> |
如果使用 archives 调用函数, 只需简单一句话, 跟上面代码的效果是相同的,代码如下:
1 | <?php get_archives("postbypost", "6", "html", " ", " "); ?> |
最热文章:
在需要添加评论最多的文章列表地方插入如下代码则可:
1 2 3 4 5 6 7 8 9 10 11 12 | <ul class="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> |
最新评论:
在需要添加最新评论的地方插入如下代码则可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?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; ?> |
如果你贴了以上代码出现错误 Parse error: syntax error, unexpected ‘=’ , 也许你可以尝试改用以下 Muki 修改过的代码:
1 2 3 4 5 6 7 8 9 10 11 12 | <?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 = 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; ?> |
出错的程式码在于「判断是否要显示密码文章的迴响」, 我所做的修改是将此段 code 作修正, 而预设密码文章的迴响不会显示在迴响栏. 假使你希望密码文章的迴响也会显示, 就将「AND comment_type = post_password 」给移除即可. 这都是 Muki 的原话~
相关文章:
是的, 就是连相关文章列表我们也不需要插件支持, 下面的代码会根据文章中的 tag标签 自动判断何篇文章与当前相关,而且相关性也很强!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <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> |
随机文章:
1 2 3 4 5 6 | <?php query_posts(array('orderby' => 'rand', 'showposts' => 2)); if (have_posts()) : while (have_posts()) : the_post();?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a> <?php comments_number('', '(1)', '(%)'); ?><br /> <?php endwhile;endif; ?> |
如果你还想显示的效果含标题以及文章摘要的话, 可以这样写:
1 2 3 4 5 6 7 8 | <?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 主题中内建这些功能其实很容易. 另外, 使用这些简单的代码不仅让主题需要使用的插件得以减少, 或者这还是那些正在寻找合适主题的朋友的愿望所在.
参考资料: Jinwen Say, Muki.
着这个比较实用
@NS基地
嗯~ 能用代码实现的还是少用插件的好啊~ 速度无止境