欢迎访问 夜阑小雨 我的学习碎片档案,这里记录了我的学习内容和工作中经验,希望给您带去帮助。

WPer必备:汇编大量WordPress代码

wordpress 夜阑小雨 789℃ 0评论

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

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

  • 显示最新文章
      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>
    1. 显示最新评论
      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;?>
    2. 显示热评文章
      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 } } ?>
    3. 显示文章分类
      1. <h2>Categories</h2>
      2. <ul>
      3. <?php wp_list_cats(‘sort_column=name’); ?>
      4. </ul>
    4. 显示归档
      1. <h2>Archives</h2>
      2. <ul>
      3. <?php wp_get_archives(‘type=monthly’); ?>
      4. </ul>
    5. 在侧栏显示页面列表
      1. <h2>Pages</h2>
      2. <ul>
      3. <?php wp_list_pages(‘title_li=’); ?>
      4. </ul>
    6. 调用Gravatar(只适应2.5以上)
      1. <br />
      1. <?php if(function_exists(’get_avatar’)){ echo get_avatar($comment, ‘50?);} ?>
    7. 显示友情链接
      1. <ul>
      2. <?php get_links_list(); ?>
      3. </ul>
    8. 显示管理员链接
      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>
    9. 在侧栏显示页面的子页面
      1. <?php$children = wp_list_pages(‘title_li=&child_of=’.$post->ID.’&echo=0′);
      2. if ($children) { ?><ul> <?php echo $children; ?>
      3. </ul>
      4. <?php } ?>
    10. 显示Wordpress标签
      1. <br />
      1. <?php the_tags(); ?>
    11. 显示Wordpress标签云
      1. <?php wp_tag_cloud(‘smallest=8&largest=36&’); ?>
    12. 模板标题
      1. <br />
      1. <?php /* Template Name: Portfolio */ ?>
    13. 动态标题标签
      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> ++CoolCode_BLOCK_5++
    14. 在独立页面中运行PHP
      1. <br />
      1. <?php if ( is_home() ) { include (‘file.php’); } ?>

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

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

    转载请注明:夜阑小雨 » WPer必备:汇编大量WordPress代码

    喜欢 (0)or分享 (0)
    发表我的评论
    取消评论

    表情

    Hi,您需要填写昵称和邮箱!

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址