說明
the_widget() 是一個模板標簽,用來顯示任何一個小工具。它可以在模板的任何地方使用。
用法
<?php the_widget($widget, $instance, $args); ?>
參數(shù)
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
(數(shù)組或query-string-style)小工具的實例設置。具體請看下面的小工具示例。
args
(數(shù)組或query-string-style)小工具的側邊欄參數(shù)
- 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
顯示每個存檔的文章數(shù)(1)。wp_get_archives 的 show_post_count 參數(shù)。默認值: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
顯示每個分類的文章數(shù)。wp_dropdown_categories 或 wp_list_categories 的 show_count 參數(shù)。默認值:0(隱藏),可選:1(顯示) - hierarchical
以層級關系顯示分類列表。默認值:0(不顯示層級),可選:1(顯示層級) - dropdown
以下拉列表顯示分類列表。默認值:0(無序列表),可選:1(下拉列表)
示例
默認用法
<?php the_widget('WP_Widget_Categories'); ?>
下拉顯示帶文章篇數(shù)的分類列表
<?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 參數(shù)。默認值:false(顯示所有鏈接分類) - description
顯示鏈接的描述。show_description 參數(shù)。 默認值:0 –false(隱藏),可選:1 – true(顯示) - rating
顯示鏈接評級。show_rating 參數(shù)。默認值:0 –false(隱藏),可選:1 – true(顯示) - images
顯示鏈接的圖片。show_images 參數(shù)。默認值:1 – true (顯示),可選:0 –false(隱藏) - name
如果顯示鏈接圖片,輸出圖片的 alt 值。show_name 參數(shù)。默認:默認值:0 –false(隱藏),可選:1 – true(顯示)
示例
默認用法
<?php the_widget('WP_Widget_Links'); ?>
顯示分類ID為 2 或 3 的鏈接
<?php the_widget('WP_Widget_Links', 'category=2,3'); ?>
Meta小工具
顯示網(wǎng)站的Meta信息(登錄、登出,管理,F(xiàn)eed 和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 參數(shù)。默認值:menu_order - exclude
頁面ID,多個ID使用英文逗號隔開。從列表中排除某些ID的頁面。默認值:null(顯示所有頁面)
示例
默認用法
<?php the_widget(‘WP_Widget_Pages’); ?>
使用<h3>Contents</h3>作為標題,根據(jù)最后修改日期排序
<?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
評論的顯示數(shù)量(最多15條)。默認:5
示例
默認用法
<?php the_widget('WP_Widget_Recent_Comments'); ?>
最新文章小工具
顯示最近發(fā)布的文章
<?php the_widget('WP_Widget_Recent_Posts', $instance, $args); ?>
- 小工具類名:widget_recent_entries
- instance:
- title
文章列表的標題。默認值:__(‘Recent Posts’) - number
要顯示的文章數(shù)量(最多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 項目的數(shù)量 - 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'); ?>
函數(shù)歷史
始用于 WordPress 2.8.0
WordPress 3.0 添加 WP_Nav_Menu_Widget
注:本文的某些小工具的介紹和使用方法還沒有補充完整,如果你有興趣,歡迎進行補充完善,投稿給我們,謝謝。
- 原文:http://codex.wordpress.org/Function_Reference/the_widget
- 編譯:倡萌@WordPress大學




