WordPress函数:the_widget(小工具)

说明

the_widget() 是一个模板标签,用来显示任何一个小工具。它可以在模板的任何地方使用。

用法

<?php the_widget($widget, $instance, $args); ?>

参数

widget

(字符串)小工具的 PHP 类名

  • WP_Widget_Archives —— Archives
  • WP_Widget_Calendar  —— Calendar
  • WP_Widget_Categories  —— Categories
  • WP_Widget_Links  —— Links
  • WP_Widget_Meta  —— Meta
  • WP_Widget_Pages  —— Pages
  • WP_Widget_Recent_Comments  —— Recent Comments
  • WP_Widget_Recent_Posts  —— Recent Posts
  • WP_Widget_RSS  ——  RSS
  • WP_Widget_Search  —— Search (a search from)
  • WP_Widget_Tag_Cloud  ——  Tag Cloud
  • WP_Widget_Text  —— Text
  • WP_Nav_Menu_Widget

instance

(数组或query-string-style)小工具的实例设置。具体请看下面的小工具示例。

args

(数组或query-string-style)小工具的侧边栏参数

  • before_widget

    (字符串)小工具前的文本或HTML代码

    默认值:<div class="widget {小工具的类名}">

  • after_widget

    (字符串)小工具后的文本或HTML代码

    默认值:</div>

  • before_title

    (字符串)小工具标题前的文本或HTML代码

    默认值:<h2 class="小工具标题">

  • after_title

    (字符串)小工具标题后的文本或HTML代码

    默认值:</h2>

存档小工具

显示每月存档列表

 &lt;?php the_widget('WP_Widget_Archives', $instance, $args); ?&gt;
  • 小工具类名:widget_archive
  • instance:
    • titl

      存档列表的标题。默认值:__(‘Archives’

    • coun

      显示每个存档的文章数(1)。wp_get_archives 的 show_post_count 参数。

      默认值:0 (隐藏

      可选:1 (显示)

    • dropdow

      以下拉菜单显示存档列

      默认值:0(无序列表)

      可选:1 (下拉列表)

示例

默认用法

&lt;?php the_widget('WP_Widget_Archives'); ?&gt;

显示下拉列表

&lt;?php the_widget('WP_Widget_Archives', 'dropdown=1'); ?&gt;

日历小工具

显示一个日历

&lt;?php the_widget('WP_Widget_Calendar', $instance, $args); ?&gt;
  • 小工具类名:widget_calendar
  • instance:
    • title

      日历的标题,默认为空。

示例

默认用法

&lt;?php the_widget('WP_Widget_Calendar'); ?&gt;

分类小工具

显示分类列表

&lt;?php the_widget('WP_Widget_Categories', $instance, $args); ?&gt;
  • 小工具类名:widget_categories
  • instance:
    • title

      分类列表的标题。默认值:__( ‘Categories’ )

    • count

      显示每个分类的文章数。wp_dropdown_categories 或 wp_list_categories 的 show_count 参数。默认值:0(隐藏),可选:1(显示)

    • hierarchical

      以层级关系显示分类列表。默认值:0(不显示层级),可选:1(显示层级)

    • dropdown

      以下拉列表显示分类列表。默认值:0(无序列表),可选:1(下拉列表)

示例

默认用法

&lt;?php the_widget('WP_Widget_Categories'); ?&gt;

下拉显示带文章篇数的分类列表

&lt;?php the_widget('WP_Widget_Categories', 'dropdown=1&amp;count=1'); ?&gt;

链接小工具

按分类显示链接

注:在 WordPress 3.5 以后默认去掉了链接管理功能,所以该小工具也默认不显示。如果要恢复,请看《恢复链接管理功能

&lt;?php the_widget('WP_Widget_Links', $instance, $args); ?&gt;
  • 小工具类名:widget_links
  • instance:
    • title

      链接小工具的标题

    • category

      链接的分类ID,多个ID使用英文逗号隔开。wp_list_bookmarks 的 category 参数。默认值:false(显示所有链接分类)

    • description

      显示链接的描述。show_description 参数。 默认值:0 –false(隐藏),可选:1 – true(显示)

    • rating

      显示链接评级。show_rating 参数。默认值:0 –false(隐藏),可选:1 – true(显示)

    • images

      显示链接的图片。show_images 参数。默认值:1 – true (显示),可选:0 –false(隐藏)

    • name

      如果显示链接图片,输出图片的 alt 值。show_name 参数。默认:默认值:0 –false(隐藏),可选:1 – true(显示)

示例

默认用法

&lt;?php the_widget('WP_Widget_Links'); ?&gt;

显示分类ID为 2 或 3 的链接

&lt;?php the_widget('WP_Widget_Links', 'category=2,3'); ?&gt;

Meta小工具

显示网站的Meta信息(登录、登出,管理,Feed 和WordPress链接)

&lt;?php the_widget('WP_Widget_Meta', $instance, $args); ?&gt;
  • 小工具类名:widget_meta
  • instance:
    • title

      Meta小工具的标题。默认值:__(‘Meta’)

示例

默认用法

<?php the_widget(‘WP_Widget_Meta’); ?>

页面小工具

显示页面列表

&lt;?php the_widget('WP_Widget_Pages', $instance, $args); ?&gt;
  • 小工具类名:widget_pages
  • instance:
    • title

      页面列表的标题。默认值:__( ‘Pages’ )

    • sortby

      排列方式。wp_list_pages 的 sort_column 参数。默认值:menu_order

    • exclude

      页面ID,多个ID使用英文逗号隔开。从列表中排除某些ID的页面。默认值:null(显示所有页面)

示例

默认用法

<?php the_widget(‘WP_Widget_Pages’); ?>

使用<h3>Contents</h3>作为标题,根据最后修改日期排序

&lt;?php the_widget('WP_Widget_Pages', 'title=Contents&amp;sortby=post_modified', 'before_title=&lt;h3&gt;&amp;after_title=&lt;/h3&gt;'); ?&gt;

最近评论小工具

显示最近的评论

&lt;?php the_widget('WP_Widget_Recent_Comments', $instance, $args); ?&gt;
  • 小工具类名:widget_recent_comments
  • instance:
    • title

      评论列表的标题。默认值:__(‘Recent Comments’)

    • number

      评论的显示数量(最多15条)。默认:5

示例

默认用法

&lt;?php the_widget('WP_Widget_Recent_Comments'); ?&gt;

最新文章小工具

显示最近发布的文章

&lt;?php the_widget('WP_Widget_Recent_Posts', $instance, $args); ?&gt;
  • 小工具类名:widget_recent_entries
  • instance:
    • title

      文章列表的标题。默认值:__(‘Recent Posts’)

    • number

      要显示的文章数量(最多15),默认:5

示例

默认用法

&lt;?php the_widget('WP_Widget_Recent_Posts'); ?&gt;

RSS小工具

显示来自任何RSS或Atom订阅的文章。

&lt;?php the_widget('WP_Widget_RSS', $instance, $args); ?&gt;
  • 小工具类名:widget_rss
  • instance:
    • title

      文章列表的标题。默认值:订阅源的标题

    • url

      包含RSS或Atom订阅的URL

    • items

      要显示的RSS 或 Atom 项目的数量

    • show_summary

      显示文章摘要

    • show_author

      显示作者

    • show_date

      显示日期

示例

默认用法

&lt;?php the_widget('WP_Widget_RSS'); ?&gt;

搜索小工具

显示一个搜索表单

&lt;?php the_widget('WP_Widget_Search', $instance, $args); ?&gt;
  • 小工具类名:widget_search
  • instance:
    • title

      搜索表单的标题。默认为空

示例

默认用法

&lt;?php the_widget('WP_Widget_Search'); ?&gt;

标签云小工具

&lt;?php the_widget( 'WP_Widget_Tag_Cloud', $instance, $args); ?&gt;
  • 小工具类名:widget_tag_cloud
  • instance:
    • title

      标签云小工具的标题。默认值:__(‘Tags’)

    • taxonomy

      要显示的分类法。默认值:post_tag(文章标签)

示例

默认用法

&lt;?php the_widget( 'WP_Widget_Tag_Cloud'); ?&gt;

文本小工具

&lt;?php the_widget('WP_Widget_Text', $instance, $args); ?&gt;
  • 小工具类名:widget_text
  • instance:
    • title

      文本小工具的标题。默认为空

    • text

      要显示的内容

    • filter

示例

默认用法

&lt;?php the_widget('WP_Widget_Text'); ?&gt;

菜单小工具

WordPress 3.0 添加的新的小工具。显示自定义菜单。

&lt;?php the_widget('WP_Widget_Nav_Menu', $instance, $args); ?&gt;
  • 小工具类名:widget_nav_menu

示例

默认用法

&lt;?php the_widget('WP_Widget_Nav_Menu'); ?&gt;

函数历史

始用于 WordPress 2.8.0

WordPress 3.0 添加 WP_Nav_Menu_Widget

注:本文的某些小工具的介绍和使用方法还没有补充完整,如果你有兴趣,欢迎进行补充完善,投稿给我们,谢谢。

  • 原文:http://codex.wordpress.org/Function_Reference/the_widget
  • 编译:倡萌@WordPress大学

来源:

https://www.wpdaxue.com/the_widget.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32285.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?