怎样在wordpress网站添加小工具

怎样在wordpress网站添加小工具,第1张

给你一个示例吧,这个示例的用途是:替代WordPress自带的“功能”小工具,因为WP自带的这个小工具有WordPress文字以及链接,这个小工具将会取消WP那个自带小工具的注册,并注册一个新的不带WordPress文字以及链接的功能小工具。

将下面的代码添加到你当前主题的functions.php中,或者自制个插件,放进去:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

function coolwp_remove_meta_widget() {

/*移除Wordpress自带的Meta小工具*/

unregister_widget('WP_Widget_Meta')

/*注册自己的Meta小工具*/

register_widget('WP_Widget_Meta_Mod')

}

add_action( 'widgets_init', 'coolwp_remove_meta_widget' )

/*

自定义小工具扩展类

*/

class WP_Widget_Meta_Mod extends WP_Widget {

function __construct() {

$widget_ops = array('classname' =>'widget_meta', 'description' =>__( "Log in/out, admin, feed and WordPress links") )

parent::__construct('meta', __('Meta'), $widget_ops)

}

function widget( $args, $instance ) {

extract($args)

$title = apply_filters('widget_title', empty($instance['title']) ? __('Meta') : $instance['title'], $instance, $this->id_base)

echo $before_widget

if ( $title )

echo $before_title . $title . $after_title

?>

<ul>

<?php wp_register()?>

<li><?php wp_loginout()?></li>

<?php wp_meta()?>

</ul>

<?php

echo $after_widget

}

function update( $new_instance, $old_instance ) {

$instance = $old_instance

$instance['title'] = strip_tags($new_instance['title'])

return $instance

}

function form( $instance ) {

$instance = wp_parse_args( (array) $instance, array( 'title' =>'' ) )

$title = strip_tags($instance['title'])

?>

<p><label for="<?php echo $this->get_field_id('title')?>"><?php _e('Title:')?></label><input class="widefat" id="<?php echo $this->get_field_id('title')?>" name="<?php echo $this->get_field_name('title')?>" type="text" value="<?php echo esc_attr($title)?>" /></p>

<?php

}

}

你可以直接使用这个小工具,同时呢,你也可以在这个小工具的基础上进行修改以满足自己的需求。

不了解再问。

祝愉快!

首先在主题文件funcitons.php中加入如下代码:

function quickchic_widgets_init() {

register_sidebar(array(

'name' => __( 'Sidebar 1', 'quickchic' ),

'id' => 'sidebar-1',

'before_widget' => '',

'after_widget' => '',

'before_title' => '<h4>',

'after_title' => '</h4>',

))

}

add_action( 'init', 'quickchic_widgets_init' )

然后在sidebar.php中加入如下代码

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>

<?php dynamic_sidebar( 'sidebar-1' ) ?>

<?php endif ?>

进入wordpress仪表盘=>外观=>小工具,添加小工具,保存,然后刷新首页就可以看到小工具的效果了

来自wordpress中文非官方论坛


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11651237.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存