文本是《一步步创建你的第一个 WordPress 小工具(共5篇)》专题的第 5 篇。阅读本文前,建议先阅读前面的文章:
创建你的WordPress小工具的最后一步是在网站上显示其输出结果。要做到这一点,你需要进一步编写WP_Widget 类。
你需要做的是
跟随本教程,你需要
- 安装一个WordPress开发环境
- 一个代码编辑器
- 来自前期教程“为你的 WordPress 小工具创建表单”中的相关代码
- 来自我之前的教程“在WordPress中创建上下文相关的侧栏导航”中的相关代码
编辑小工具的输出结果
这个过程分两步:一,在WordPress小工具之外添加一个函数,确认初始页面有效;二,在 WP_Widget 类中编辑 widget 函数。
添加“初始函数”
这个函数是从我之前创建一个“上下文相关的侧栏导航”插件的课程中直接引入的。
在你的 WP_Widget 类上,将以下函数添加到你的插件文件中。
- <?<span%20class=”pln”>php
- function<span%20class=”pln”> tutsplus_check_for_page_tree<span%20class=”pun”>() {
- //首先检测当前访问的是否是一个页面
- if<span%20class=”pun”>(<span%20class=”pln”> is_page<span%20class=”pun”>() ) {
- global<span%20class=”pln”> $post<span%20class=”pun”>;
- // 接着检测该页面是否有父级页面
- if (<span%20class=”pln”> $post<span%20class=”pun”>-><span%20class=”pln”>post_parent <span%20class=”pun”>){
- // 获取父级页面名单
- $parents <span%20class=”pun”>=<span%20class=”pln”> array_reverse<span%20class=”pun”>(<span%20class=”pln”> get_post_ancestors<span%20class=”pun”>(<span%20class=”pln”> $post<span%20class=”pun”>-><span%20class=”pln”>ID <span%20class=”pun”>) );
- // 获取最顶级页面
- return<span%20class=”pln”> $parents<span%20class=”pun”>[<span%20class=”lit”>0<span%20class=”pun”>];
- }
- // 返回ID – 如果存在父级页面,就返回最顶级的页面ID,否则返回当前页面ID, or the current page if not
- return<span%20class=”pln”> $post<span%20class=”pun”>-><span%20class=”pln”>ID<span%20class=”pun”>;
- }
- }
- ?>
稍后当定义在小工具上运行的查询功能时,这个函数也会用到。
编辑widget函数
下一步你需要做的便是在你的插件文件中编辑你之前就建立的但还未填充的widget 函数。从定义基于表单输入的变量开始:
- function<span%20class=”pln”> widget<span%20class=”pun”>(<span%20class=”pln”> $args<span%20class=”pun”>,<span%20class=”pln”> $instance <span%20class=”pun”>) {
- extract<span%20class=”pun”>(<span%20class=”pln”> $args <span%20class=”pun”>);
- echo $before_widget<span%20class=”pun”>;
- echo $before_title <span%20class=”pun”>. ‘In this section:’ .<span%20class=”pln”> $after_title<span%20class=”pun”>;
- }
下一步,添加查询和输出,函数编辑如下:
- function<span%20class=”pln”> widget<span%20class=”pun”>(<span%20class=”pln”> $args<span%20class=”pun”>,<span%20class=”pln”> $instance <span%20class=”pun”>) {
- // kick things off
- extract<span%20class=”pun”>(<span%20class=”pln”> $args <span%20class=”pun”>);
- echo $before_widget<span%20class=”pun”>;
- echo $before_title <span%20class=”pun”>. ‘In this section:’ .<span%20class=”pln”> $after_title<span%20class=”pun”>;
- // run a query if on a page
- if (<span%20class=”pln”> is_page<span%20class=”pun”>() ) {
- // run the tutsplus_check_for_page_tree function to fetch top level page
- $ancestor <span%20class=”pun”>=<span%20class=”pln”> tutsplus_check_for_page_tree<span%20class=”pun”>();
- // set the arguments for children of the ancestor page
- $args <span%20class=”pun”>=<span%20class=”pln”> array<span%20class=”pun”>(
- ‘child_of’ =><span%20class=”pln”> $ancestor<span%20class=”pun”>,
- ‘depth’ =><span%20class=”pln”> $instance<span%20class=”pun”>[ ‘depth’ ],
- ‘title_li’ => ”<span%20class=”pun”>,
- );
- // set a value for get_pages to check if it’s empty
- $list_pages <span%20class=”pun”>=<span%20class=”pln”> get_pages<span%20class=”pun”>(<span%20class=”pln”> $args <span%20class=”pun”>);
- // check if $list_pages has values
- if<span%20class=”pun”>(<span%20class=”pln”> $list_pages <span%20class=”pun”>) {
- // open a list with the ancestor page at the top
- ?>
- <<span%20class=”pln”>ul <span%20class=”kwd”>class<span%20class=”pun”>=<span%20class=”str”>”page-tree”<span%20class=”pun”>>
- <?<span%20class=”pln”>php <span%20class=”com”>// list ancestor page ?>
- <<span%20class=”pln”>li <span%20class=”kwd”>class<span%20class=”pun”>=<span%20class=”str”>”ancestor”<span%20class=”pun”>>
- <<span%20class=”pln”>a href<span%20class=”pun”>=<span%20class=”str”>”<?php echo get_permalink( $ancestor ); ?>”<span%20class=”pun”>><?<span%20class=”pln”>php echo get_the_title<span%20class=”pun”>(<span%20class=”pln”> $ancestor <span%20class=”pun”>); ?></<span%20class=”pln”>a<span%20class=”pun”>>
- </<span%20class=”pln”>li<span%20class=”pun”>>
- <?<span%20class=”pln”>php
- // use wp_list_pages to list subpages of ancestor or current page
- wp_list_pages<span%20class=”pun”>(<span%20class=”pln”> $args <span%20class=”pun”>);;
- // close the page-tree list
- ?>
- </<span%20class=”pln”>ul<span%20class=”pun”>>
- <?<span%20class=”pln”>php
- }
- }
- }
首先这会检查我们是否在当前页面上,然后根据之前相关函数的输出和 $depth变量的值(在WordPress小工具表单中已设置好),定义list_pages()函数的参数。
现在保存你的小工具并检查你的网址。无论是否添加小工具,你的目录应该会在网页上显示出来。
最终的插件
现在你终于拥有一个完整的WordPress小工具插件了!
回顾你在5个教程中所涉及到的内容,插件代码全文应如下所示:
- <?<span%20class=”pln”>php
- /*Plugin Name: List Subpages Widget
- Description: This widget checks if the current page has parent or child pages and if so, outputs a list of the highest ancestor page and its descendants. This file supports part 5 of the series to create the widget and doesn’t give you a functioning widget.
- Version: 0.5
- Author: Rachel McCollin
- Author URI: http://rachelmccollin.com
- License: GPLv2
- */
- ?>
- <?<span%20class=”pln”>php
- ?>
- <?<span%20class=”pln”>php
- /*******************************************************************************
- function tutsplus_check_for_page_tree() – checks if the current page is in a page tree.
- *******************************************************************************/
- ?>
- <?<span%20class=”pln”>php
- function<span%20class=”pln”> tutsplus_check_for_page_tree<span%20class=”pun”>() {
- //start by checking if we’re on a page
- if<span%20class=”pun”>(<span%20class=”pln”> is_page<span%20class=”pun”>() ) {
- global<span%20class=”pln”> $post<span%20class=”pun”>;
- // next check if the page has parents
- if (<span%20class=”pln”> $post<span%20class=”pun”>-><span%20class=”pln”>post_parent <span%20class=”pun”>){
- // fetch the list of ancestors
- $parents <span%20class=”pun”>=<span%20class=”pln”> array_reverse<span%20class=”pun”>(<span%20class=”pln”> get_post_ancestors<span%20class=”pun”>(<span%20class=”pln”> $post<span%20class=”pun”>-><span%20class=”pln”>ID <span%20class=”pun”>) );
- // get the top level ancestor
- return<span%20class=”pln”> $parents<span%20class=”pun”>[<span%20class=”lit”>0<span%20class=”pun”>];
- }
- // return the id – this will be the topmost ancestor if there is one, or the current page if not
- return<span%20class=”pln”> $post<span%20class=”pun”>-><span%20class=”pln”>ID<span%20class=”pun”>;
- }
- }
- ?>
- <?<span%20class=”pln”>php
- class Tutsplus_List_Pages_Widget extends<span%20class=”pln”> WP_Widget <span%20class=”pun”>{
- function<span%20class=”pln”> __construct<span%20class=”pun”>() {
- parent<span%20class=”pun”>::<span%20class=”pln”>__construct<span%20class=”pun”>(
- // base ID of the widget
- ‘tutsplus_list_pages_widget’<span%20class=”pun”>,
- // name of the widget
- __<span%20class=”pun”>(<span%20class=”str”>’List Related Pages'<span%20class=”pun”>, ‘tutsplus’ ),
- // widget options
- array <span%20class=”pun”>(
- ‘description’ =><span%20class=”pln”> __<span%20class=”pun”>( ‘Identifies where the current page is in the site structure and displays a list of pages in the same section of the site. Only works on Pages.’<span%20class=”pun”>, ‘tutsplus’ )
- )
- );
- }
- function<span%20class=”pln”> form<span%20class=”pun”>(<span%20class=”pln”> $instance <span%20class=”pun”>) {
- $defaults <span%20class=”pun”>=<span%20class=”pln”> array<span%20class=”pun”>(
- ‘depth’ => ‘-1’
- );
- $depth <span%20class=”pun”>=<span%20class=”pln”> $instance<span%20class=”pun”>[ ‘depth’ ];
- // markup for form <span%20class=”pun”>?>
- <p>
- <label for=”<span%20class=”pun”><?<span%20class=”pln”>php echo $this<span%20class=”pun”>-><span%20class=”pln”>get_field_id<span%20class=”pun”>( ‘depth’ ); ?><span%20class=”pln”>”>Depth of list:<span%20class=”tag”></label>
- <input class=”widefat” type=”text” id=”<span%20class=”pun”><?<span%20class=”pln”>php echo $this<span%20class=”pun”>-><span%20class=”pln”>get_field_id<span%20class=”pun”>( ‘depth’ ); ?><span%20class=”pln”>” name=”<span%20class=”pun”><?<span%20class=”pln”>php echo $this<span%20class=”pun”>-><span%20class=”pln”>get_field_name<span%20class=”pun”>( ‘depth’ ); ?><span%20class=”pln”>” value=”<span%20class=”pun”><?<span%20class=”pln”>php echo esc_attr<span%20class=”pun”>(<span%20class=”pln”> $depth <span%20class=”pun”>); ?><span%20class=”pln”>”>
- </p>
- <?<span%20class=”pln”>php
- }
- function<span%20class=”pln”> update<span%20class=”pun”>(<span%20class=”pln”> $new_instance<span%20class=”pun”>,<span%20class=”pln”> $old_instance <span%20class=”pun”>) {
- $instance <span%20class=”pun”>=<span%20class=”pln”> $old_instance<span%20class=”pun”>;
- $instance<span%20class=”pun”>[ ‘depth’ ] =<span%20class=”pln”> strip_tags<span%20class=”pun”>(<span%20class=”pln”> $new_instance<span%20class=”pun”>[ ‘depth’ ] );
- return<span%20class=”pln”> $instance<span%20class=”pun”>;
- }
- function<span%20class=”pln”> widget<span%20class=”pun”>(<span%20class=”pln”> $args<span%20class=”pun”>,<span%20class=”pln”> $instance <span%20class=”pun”>) {
- // kick things off
- extract<span%20class=”pun”>(<span%20class=”pln”> $args <span%20class=”pun”>);
- echo $before_widget<span%20class=”pun”>;
- echo $before_title <span%20class=”pun”>. ‘In this section:’ .<span%20class=”pln”> $after_title<span%20class=”pun”>;
- // run a query if on a page
- if (<span%20class=”pln”> is_page<span%20class=”pun”>() ) {
- // run the tutsplus_check_for_page_tree function to fetch top level page
- $ancestor <span%20class=”pun”>=<span%20class=”pln”> tutsplus_check_for_page_tree<span%20class=”pun”>();
- // set the arguments for children of the ancestor page
- $args <span%20class=”pun”>=<span%20class=”pln”> array<span%20class=”pun”>(
- ‘child_of’ =><span%20class=”pln”> $ancestor<span%20class=”pun”>,
- ‘depth’ =><span%20class=”pln”> $instance<span%20class=”pun”>[ ‘depth’ ],
- ‘title_li’ => ”<span%20class=”pun”>,
- );
- // set a value for get_pages to check if it’s empty
- $list_pages <span%20class=”pun”>=<span%20class=”pln”> get_pages<span%20class=”pun”>(<span%20class=”pln”> $args <span%20class=”pun”>);
- // check if $list_pages has values
- if<span%20class=”pun”>(<span%20class=”pln”> $list_pages <span%20class=”pun”>) {
- // open a list with the ancestor page at the top
- ?>
- <ul class<span%20class=”pun”>=<span%20class=”atv”>”page-tree”<span%20class=”tag”>>
- <?<span%20class=”pln”>php <span%20class=”com”>// list ancestor page <span%20class=”pun”>?>
- <li class<span%20class=”pun”>=<span%20class=”atv”>”ancestor”<span%20class=”tag”>>
- <a href=”<span%20class=”pun”><?<span%20class=”pln”>php echo get_permalink<span%20class=”pun”>(<span%20class=”pln”> $ancestor <span%20class=”pun”>); ?><span%20class=”pln”>”><span%20class=”pun”><?<span%20class=”pln”>php echo get_the_title<span%20class=”pun”>(<span%20class=”pln”> $ancestor <span%20class=”pun”>); ?><span%20class=”tag”></a>
- </li>
- <?<span%20class=”pln”>php
- // use wp_list_pages to list subpages of ancestor or current page
- wp_list_pages<span%20class=”pun”>(<span%20class=”pln”> $args <span%20class=”pun”>);;
- // close the page-tree list
- ?>
- </ul>
- <?<span%20class=”pln”>php
- }
- }
- }
- }
- ?>
- <?<span%20class=”pln”>php
- /*******************************************************************************
- function tutsplus_register_list_pages_widget() – registers the widget.
- *******************************************************************************/
- ?>
- <?<span%20class=”pln”>php
- function<span%20class=”pln”> tutsplus_register_list_pages_widget<span%20class=”pun”>() {
- register_widget<span%20class=”pun”>( ‘Tutsplus_List_Pages_Widget’ );
- }
- add_action<span%20class=”pun”>( ‘widgets_init’<span%20class=”pun”>, ‘tutsplus_register_list_pages_widget’ );
- ?>
小结
创建一个WordPress小工具应该包含以下几个步骤:
- 注册你的小工具
- 创建类来保存widget函数
- 编写一个construct函数来构建你的小工具
- 为小工具界面上的表单编写一个form函数
- 编写一个update函数以便小工具能够在表单中及时更新
- 编写一个定义输出的 widget函数
一旦你完成了以上步骤,你就创建了一个可以正常运行的WordPress小工具,并且你还可以根据自己的需要加以改编哦。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 lk2768783601@gmail.com 举报,一经查实,本站将立刻删除。