首页/ 问答/ 正文

WordPress中的小工具有哪些?

注册会员 / 悠然自学 / 2023-06-11/ 浏览 78 次

WordPress中的小工具有:

  1. 文本(Text)小工具:允许在侧边栏或其他小工具区域添加自定义文本、HTML、CSS代码等。

  2. 近期文章(Recent Posts)小工具:可以显示最近发布的文章列表,可设置要显示的文章数目。

  3. 近期评论(Recent Comments)小工具:可以显示最近的评论列表,可设置要显示的评论数目。

  4. 网站分类(Categories)小工具:可以显示网站已有的分类目录,方便用户快速浏览。

  5. 网站链接(Links)小工具:可以显示网站常用链接,比如友情链接等。

  6. 归档(Archives)小工具:可以显示按月份或按年份分组的文章列表,方便用户查看历史文章。

示例代码:

  1. <!-- 文本(Text)小工具 -->
  2. <div class="widget widget_text">
  3. <h2 class="widget-title">自定义文本</h2>
  4. <div class="textwidget">
  5. <p>这里可以添加自定义文本、HTML、CSS代码等。</p>
  6. </div>
  7. </div>
  8. <!-- 近期文章(Recent Posts)小工具 -->
  9. <div class="widget widget_recent_entries">
  10. <h2 class="widget-title">近期文章</h2>
  11. <ul>
  12. <?php
  13. $recent_posts = wp_get_recent_posts(array(
  14. 'numberposts' => 5,
  15. 'post_status' => 'publish'
  16. ));
  17. foreach ($recent_posts as $post) :
  18. ?>
  19. <li>
  20. <a href="<?php echo get_permalink($post['ID']); ?>">
  21. <?php echo $post['post_title']; ?>
  22. </a>
  23. </li>
  24. <?php
  25. endforeach;
  26. ?>
  27. </ul>
  28. </div>
  29. <!-- 近期评论(Recent Comments)小工具 -->
  30. <div class="widget widget_recent_comments">
  31. <h2 class="widget-title">近期评论</h2>
  32. <ul>
  33. <?php
  34. $recent_comments = get_comments(array(
  35. 'number' => 5,
  36. 'status' => 'approve'
  37. ));
  38. foreach ($recent_comments as $comment) :
  39. ?>
  40. <li>
  41. <a href="<?php echo get_comment_link($comment->comment_ID); ?>">
  42. <?php echo get_comment_author($comment->comment_ID); ?>:
  43. <?php echo wp_trim_words(strip_tags($comment->comment_content), 10); ?>
  44. </a>
  45. </li>
  46. <?php
  47. endforeach;
  48. ?>
  49. </ul>
  50. </div>
  51. <!-- 网站分类(Categories)小工具 -->
  52. <div class="widget widget_categories">
  53. <h2 class="widget-title">网站分类</h2>
  54. <ul>
  55. <?php wp_list_categories(array(
  56. 'title_li' => ''
  57. )); ?>
  58. </ul>
  59. </div>
  60. <!-- 网站链接(Links)小工具 -->
  61. <div class="widget widget_links">
  62. <h2 class="widget-title">友情链接</h2>
  63. <ul>
  64. <?php
  65. $args = array(
  66. 'orderby' => 'name',
  67. 'category' => 0,
  68. 'categorize' => 0,
  69. 'title_li' => ''
  70. );
  71. wp_list_bookmarks($args);
  72. ?>
  73. </ul>
  74. </div>
  75. <!-- 归档(Archives)小工具 -->
  76. <div class="widget widget_archive">
  77. <h2 class="widget-title">文章归档</h2>
  78. <ul>
  79. <?php wp_get_archives(array(
  80. 'type' => 'monthly',
  81. 'limit' => 12
  82. )); ?>
  83. </ul>
  84. </div>
大家谈论
    我的见解