wordpress小工具自定义Html,在正确的工具区域内显示你创建的WordPress小工具

/*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

*/

?>

?>

/*******************************************************************************

function tutsplus_check_for_page_tree() - checks if the current page is in a page tree.

*******************************************************************************/

?>

function tutsplus_check_for_page_tree() {

//start by checking if we're on a page

if( is_page() ) {

global $post;

// next check if the page has parents

if ( $post->post_parent ){

// fetch the list of ancestors

$parents = array_reverse( get_post_ancestors( $post->ID ) );

// get the top level ancestor

return $parents[0];

}

// return the id - this will be the topmost ancestor if there is one, or the current page if not

return $post->ID;

}

}

?>

class Tutsplus_List_Pages_Widget extends WP_Widget {

function __construct() {

parent::__construct(

// base ID of the widget

'tutsplus_list_pages_widget',

// name of the widget

__('List Related Pages', 'tutsplus' ),

// widget options

array (

'description' => __( '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.', 'tutsplus' )

)

);

}

function form( $instance ) {

$defaults = array(

'depth' => '-1'

);

$depth = $instance[ 'depth' ];

// markup for form ?>

Depth of list:

}

function update( $new_instance, $old_instance ) {

$instance = $old_instance;

$instance[ 'depth' ] = strip_tags( $new_instance[ 'depth' ] );

return $instance;

}

function widget( $args, $instance ) {

// kick things off

extract( $args );

echo $before_widget;

echo $before_title . 'In this section:' . $after_title;

// run a query if on a page

if ( is_page() ) {

// run the tutsplus_check_for_page_tree function to fetch top level page

$ancestor = tutsplus_check_for_page_tree();

// set the arguments for children of the ancestor page

$args = array(

'child_of' => $ancestor,

'depth' => $instance[ 'depth' ],

'title_li' => '',

);

// set a value for get_pages to check if it's empty

$list_pages = get_pages( $args );

// check if $list_pages has values

if( $list_pages ) {

// open a list with the ancestor page at the top

?>

// use wp_list_pages to list subpages of ancestor or current page

wp_list_pages( $args );;

// close the page-tree list

?>

}

}

}

}

?>

/*******************************************************************************

function tutsplus_register_list_pages_widget() - registers the widget.

*******************************************************************************/

?>

function tutsplus_register_list_pages_widget() {

register_widget( 'Tutsplus_List_Pages_Widget' );

}

add_action( 'widgets_init', 'tutsplus_register_list_pages_widget' );

?>

/*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

*/

?>

?>

/*******************************************************************************

function tutsplus_check_for_page_tree() - checks if the current page is in a page tree.

*******************************************************************************/

?>

function tutsplus_check_for_page_tree() {

//start by checking if we're on a page

if( is_page() ) {

global $post;

// next check if the page has parents

if ( $post->post_parent ){

// fetch the list of ancestors

$parents = array_reverse( get_post_ancestors( $post->ID ) );

// get the top level ancestor

return $parents[0];

}

// return the id - this will be the topmost ancestor if there is one, or the current page if not

return $post->ID;

}

}

?>

class Tutsplus_List_Pages_Widget extends WP_Widget {

function __construct() {

parent::__construct(

// base ID of the widget

'tutsplus_list_pages_widget',

// name of the widget

__('List Related Pages', 'tutsplus' ),

// widget options

array (

'description' => __( '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.', 'tutsplus' )

)

);

}

function form( $instance ) {

$defaults = array(

'depth' => '-1'

);

$depth = $instance[ 'depth' ];

// markup for form ?>

Depth of list:

}

function update( $new_instance, $old_instance ) {

$instance = $old_instance;

$instance[ 'depth' ] = strip_tags( $new_instance[ 'depth' ] );

return $instance;

}

function widget( $args, $instance ) {

// kick things off

extract( $args );

echo $before_widget;

echo $before_title . 'In this section:' . $after_title;

// run a query if on a page

if ( is_page() ) {

// run the tutsplus_check_for_page_tree function to fetch top level page

$ancestor = tutsplus_check_for_page_tree();

// set the arguments for children of the ancestor page

$args = array(

'child_of' => $ancestor,

'depth' => $instance[ 'depth' ],

'title_li' => '',

);

// set a value for get_pages to check if it's empty

$list_pages = get_pages( $args );

// check if $list_pages has values

if( $list_pages ) {

// open a list with the ancestor page at the top

?>

// use wp_list_pages to list subpages of ancestor or current page

wp_list_pages( $args );;

// close the page-tree list

?>

}

}

}

}

?>

/*******************************************************************************

function tutsplus_register_list_pages_widget() - registers the widget.

*******************************************************************************/

?>

function tutsplus_register_list_pages_widget() {

register_widget( 'Tutsplus_List_Pages_Widget' );

}

add_action( 'widgets_init', 'tutsplus_register_list_pages_widget' );

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值