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

    分类:WordPress, theme skill | 标签: ,, | 浏览次数:4,332

    当我们在寻找一个合适的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 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>

    相关文章:

    是的,就是连相关文章列表我们也不需要插件支持,下面的代码会根据文章中的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主题中内建这些功能其实很容易。另外,使用这些简单的代码不仅让主题需要使用的插件得以减少,或者这还是那些正在寻找合适主题的朋友的愿望所在。

    或者收藏到 Delicious建议通过 RSS 订阅本站更新。你还可以 follow me 谢谢! — by Jinwen @ 2009/04/15 16:46