作者:sofish 来源:幸福收藏夹   酷勤网收集 2008-05-19

摘要
  Wordpress代码片断:显示最新文章、显示最新评论、显示热评文章、显示文章分类、在侧栏显示页面列表、显示友情链接、显示管理员链接、在侧栏显示页面的子页面、显示Wordpress标签、显示Wordpress标签云、模板标题、动态标题标签、在独立页面中运行PHP

如果你是一个设计者,或只是喜欢自己修改现在所用的主题,收集你需要的Wordpress代码可能是一个很麻烦的事。从开始到现在,收集这些代码片断我花了大量的时间,我坚定认为如果把它全部写在一篇文章上,作为参考,那可能会更方便&有用。

WordPress Code

下面这些是我成功收集到的代码片断。如果你有Wordpress需要我添加的,请在下面留下言。

  1. 显示最新文章

     

    1. <?php query_posts('showposts=5'); ?>
    2. <ul>
    3. <?php while (have_posts()) : the_post(); ?>
    4. <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    5. <?php endwhile;?>
    6. </ul>

     

  2. 显示最新评论

     

    1. <?php
    2. global $wpdb;
    3. $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
    4. comment_post_ID, comment_author, comment_date_gmt, comment_approved,
    5. comment_type,comment_author_url,
    6. SUBSTRING(comment_content,1,30) AS com_excerpt
    7. FROM $wpdb->comments
    8. LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
    9. $wpdb->posts.ID)
    10. WHERE comment_approved = '1' AND comment_type = '' AND
    11. post_password = ''
    12. ORDER BY comment_date_gmt DESC
    13. LIMIT 10";
    14. $comments = $wpdb->get_results($sql);
    15. $output = $pre_HTML;
    16. $output .= "\n<ul>";
    17. foreach ($comments as $comment) {
    18. $output .= "\n<li>".strip_tags($comment->comment_author)
    19. .":" . "<a href=\"" . get_permalink($comment->ID) .
    20. "#comment-" . $comment->comment_ID . "\" title=\"on " .
    21. $comment->post_title . "\">" . strip_tags($comment->com_excerpt)
    22. ."</a></li>";
    23. }
    24. $output .= "\n</ul>";
    25. $output .= $post_HTML;
    26. echo $output;?>

     

  3. 显示热评文章

     

    1. <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10");
    2. foreach ($result as $topten) {
    3. $postid = $topten->ID;
    4. $title = $topten->post_title;
    5. $commentcount = $topten->comment_count;
    6. if ($commentcount != 0) { ?>
    7. <li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></li>
    8. <?php } } ?>

     

  4. 显示文章分类

     

    1. <ul>
    2. <?php wp_list_cats('sort_column=name'); ?>
    3. </ul>

     

  5. 显示归档

     

    1. <ul>
    2. <?php wp_get_archives('type=monthly'); ?>
    3. </ul>

     

  6. 在侧栏显示页面列表

     

    1. <h4>Pages</h4>
    2. <ul>
    3. <?php wp_list_pages('title_li='); ?>
    4. </ul>

     

  7. 调用Gravatar(只适应2.5以上)

     

    1. <?php if(function_exists(’get_avatar’)){ echo get_avatar($comment, ‘50?);} ?>

     

  8. 显示友情链接

     

    1. <ul>
    2. <?php get_links_list(); ?>
    3. </ul>

     

  9. 显示管理员链接

     

    1. <ul>
    2. <?php wp_register(); ?>
    3. <li><?php wp_loginout(); ?></li>
    4. <li><a href="http://www.wordpress.org/">WordPress</a></li>
    5. <?php wp_meta(); ?>
    6. <li><a href="http://validator.w3.org/check?uri=referer">XHTML</a></li>
    7. </ul>

     

  10. 在侧栏显示页面的子页面

     

    1. <?php$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');if ($children) { ?><ul> <?php echo $children; ?>
    2. </ul>
    3. <?php } ?>

     

  11. 显示Wordpress标签

     

    1. <?php the_tags(); ?>

     

  12. 显示Wordpress标签云

     

    1. <?php wp_tag_cloud('smallest=8&largest=36&'); ?>

     

  13. 模板标题

     

    1. <?php /* Template Name: Portfolio */ ?> //显示模板的名称

     

  14. 动态标题标签

     

    1. <title><?phpif (is_home()) { echo bloginfo('name');
    2. } elseif (is_404()) {
    3. echo '404 Not Found';
    4. } elseif (is_category()) {
    5. echo 'Category:'; wp_title('');
    6. } elseif (is_search()) {
    7. echo 'Search Results';
    8. } elseif ( is_day() || is_month() || is_year() ) {
    9. echo 'Archives:'; wp_title('');
    10. } else {
    11. echo wp_title('');
    12. }
    13. ?></title>

     

  15. 在独立页面中运行PHP

     

    1. <?php if ( is_home() ) { include ('file.php'); } ?>
    2. // 记得把home改成你的页面名称

     

我希望这个列表可以不会过时,如果遇到更有用的代码,我将会更新这篇文章。

原文:Huge Compilation of WordPress Code
译文:WPer必备:汇编大量Wordpress代码

分类: 站长经验 Web开发 网页设计



关于酷勤 | 联系方式 | 免责声明 | 友情链接