外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>WordPress开发>WordPress函数:the_widget(小工具)

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>

存档小工具

显示每月存档列表

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

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

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

      默认值:0 (隐藏

      可选:1 (显示)

    • dropdow

      以下拉菜单显示存档列

      默认值:0(无序列表)

      可选:1 (下拉列表)

示例

默认用法

<?php the_widget('WP_Widget_Archives'); ?> 

显示下拉列表

<?php the_widget('WP_Widget_Archives', 'dropdown=1'); ?> 

日历小工具

显示一个日历

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

      日历的标题,默认为空。

示例

默认用法

<?php the_widget('WP_Widget_Calendar'); ?> 

分类小工具

显示分类列表

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

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

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

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

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

示例

默认用法

<?php the_widget('WP_Widget_Categories'); ?> 

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

<?php the_widget('WP_Widget_Categories', 'dropdown=1&count=1'); ?> 

链接小工具

按分类显示链接

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

<?php the_widget('WP_Widget_Links', $instance, $args); ?> 
  • 小工具类名: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(显示)

示例

默认用法

<?php the_widget('WP_Widget_Links'); ?> 

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

<?php the_widget('WP_Widget_Links', 'category=2,3'); ?> 

Meta小工具

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

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

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

示例

默认用法

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

页面小工具

显示页面列表

<?php the_widget('WP_Widget_Pages', $instance, $args); ?> 
  • 小工具类名: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>作为标题,根据最后修改日期排序

<?php the_widget('WP_Widget_Pages', 'title=Contents&sortby=post_modified', 'before_title=<h3>&after_title=</h3>'); ?> 

最近评论小工具

显示最近的评论

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

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

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

示例

默认用法

<?php the_widget('WP_Widget_Recent_Comments'); ?> 

最新文章小工具

显示最近发布的文章

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

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

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

示例

默认用法

<?php the_widget('WP_Widget_Recent_Posts'); ?> 

RSS小工具

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

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

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

      包含RSS或Atom订阅的URL
    • items

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

      显示文章摘要
    • show_author

      显示作者
    • show_date

      显示日期

示例

默认用法

<?php the_widget('WP_Widget_RSS'); ?> 

搜索小工具

显示一个搜索表单

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

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

示例

默认用法

<?php the_widget('WP_Widget_Search'); ?> 

标签云小工具

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

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

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

示例

默认用法

<?php the_widget( 'WP_Widget_Tag_Cloud'); ?> 

文本小工具

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

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

      要显示的内容
    • filter

示例

默认用法

<?php the_widget('WP_Widget_Text'); ?> 

菜单小工具

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

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

示例

默认用法

<?php the_widget('WP_Widget_Nav_Menu'); ?>

函数历史

始用于 WordPress 2.8.0

WordPress 3.0 添加 WP_Nav_Menu_Widget

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

  • 原文:http://codex.wordpress.org/Function_Reference/the_widget
  • 编译:倡萌@WordPress大学
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
WordPress开发

WordPress函数:is_active_widget(已使用小工具)

2013-5-5 9:14:47

WordPress开发

WordPress函数:wp_page_menu(页面菜单)

2013-5-11 8:28:24

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索