无法将$args[]接收到我的自定义小部件

时间:2016-01-08 作者:Vlad

我是WordPress的新手。我创建了第一个丑陋的小部件。它位于wp-content/plugins/my-awesome-widget/widget_init.php (此文件夹中没有其他文件)。它正在发挥作用。我创建了一个侧栏并在中注册了它functions.php 在我的主题文件夹中:

function register_page_widgets()
{
    if (function_exists(\'register_sidebar\')) {
        register_sidebar(
            array(
                \'name\' => \'Page sidebar\',
                \'id\' => \'page_sidebar\',
                \'description\' => \'\',
                \'class\' => \'\',
                \'before_widget\' => \'<div class="page-sidebar">\',
                \'before_title\' => \'<div class="title"><span>\',
                \'after_title\' => \'</span></div>\',
                \'after_widget\' => \'</div>\'
            )
        );
    }
}

add_action( \'widgets_init\', \'register_page_widgets\' );
除了我的小部件之外,我在数组中定义的所有小部件(位于此侧边栏中,包装在元素中):在\\u小部件之前、在\\u标题之前等。在我的代码中(在widget_init.php) 我写道:

print_r($args);
结果是:

Array
(
    [name] => Page sidebar
    [id] => page_sidebar
    [description] => 
    [class] => 
    [before_widget] =>     
    [after_widget] =>     
    [before_title] =>     
    [after_title] =>     
    [widget_id] => my_first_widget
    [widget_name] => My First Widget
)
不知何故,在\\u小部件之前、在\\u小部件之后、在\\u标题之前和在\\u标题之后都是空的。怎么了?

我的代码:

<?php
/*
Plugin Name: My Own Recent Posts
Plugin URI: http://www.example.com/textwidget
Description: An example plugin to demonstrate widgets API in WordPress
Version: 0.1
Author: Author Name
Author URI: http://www.example.com
License: GPL2


*/
add_action( \'widgets_init\', function(){
    register_widget( \'My_Own_Recent_Posts\' );
});

class My_Own_Recent_Posts extends WP_Widget {

    public function __construct() {
        parent::__construct(
            \'My_Own_Recent_Posts\', 
            \'My First Widget\', 
            array( \'description\' => __( \'My First Widget\', \'text_domain\' ), ) 
        );
    }


    public function update( $new_instance, $old_instance ) {
        $instance = array(); 
        $instance[\'title\'] = sanitize_text_field( $new_instance[\'title\'] );

        $instance[\'count_of_posts\'] = sanitize_text_field( $new_instance[\'count_of_posts\'] );

        $instance[\'words_to_display\'] = sanitize_text_field( $new_instance[\'words_to_display\'] );

        $instance[\'post_types\'] =  $new_instance[\'post_types\'];

        return $instance;
    }


    public function form( $instance ) { ?>
        <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 $instance[\'title\']; ?>" />
        </p>

        <p><label for="<?php echo $this->get_field_id( \'count_of_posts\' ); ?>"><?php _e( \'Count of posts:\' ); ?></label>
            <input class="widefat"
                   id="<?php echo $this->get_field_id( \'count_of_posts\' ); ?>"
                   name="<?php echo $this->get_field_name( \'count_of_posts\' ); ?>"
                   type="number"
                   max="50"
                   min="1"
                   value="<?php echo $instance[\'count_of_posts\']; ?>" />
        </p>

        <p><label for="<?php echo $this->get_field_id( \'words_to_display\' ); ?>"><?php _e( \'Words from post to display:\' ); ?></label>
            <input class="widefat"
                   id="<?php echo $this->get_field_id( \'words_to_display\' ); ?>"
                   name="<?php echo $this->get_field_name( \'words_to_display\' ); ?>"
                   type="number"
                   max="50"
                   min="1"
                   value="<?php echo $instance[\'words_to_display\']; ?>" />
        </p>

        <p><label for="<?php echo $this->get_field_id( \'post_types\' ); ?>"><?php _e( \'Select post types:\' ); ?><br></label>
            <?php
                $args = array(\'_builtin\' => \'\', \'public\' => true);
                $post_types = get_post_types($args, \'object\');

                foreach($post_types as $post_type) : ?>
                <?php
                    $checked=\'\';
                    if(@in_array($post_type->name, $instance[\'post_types\'])) $checked=\'checked\';
                ?>
                <input type="checkbox"
                       value="<?=$post_type->name?>"
                       name="<?php echo $this->get_field_name( \'post_types\' ).\'[]\'; ?>"
                       id="<?php echo $this->get_field_id(\'post_types\'); ?>"
                       <?=$checked?>><?=$post_type->labels->name?><br>
        <?  endforeach;

    }


    public function widget( $args, $instance ) {

        echo \'<div class="page-sidebar"><div class="title"><span>\'.$instance[\'title\'].\'</span></div>\';

        function limit_words($string, $word_limit) {
            $words=explode(" ",$string);
            return implode(" ",array_splice($words,0,$word_limit));
        }

        function link_words($limited_string){
            $words = explode(\' \', $limited_string);
            array_splice($words, 5, 0, "</a>");
            return implode(\' \', $words);
        }

        if(!empty($instance)) :
            global $post;
            $posts = get_posts(
                array(
                    \'numberposts\'     => $instance[\'count_of_posts\'],
                    \'offset\'          => 0,
                    \'category\'        => \'\',
                    \'orderby\'         => \'post_date\',
                    \'order\'           => \'DESC\',
                    \'include\'         => \'\',
                    \'exclude\'         => \'\',
                    \'meta_key\'        => \'\',
                    \'meta_value\'      => \'\',
                    \'post_type\'       => $instance[\'post_types\'],
                    \'post_mime_type\'  => \'\',
                    \'post_parent\'     => \'\',
                    \'post_status\'     => \'publish\'
                )
            );

            $shows=0;
            foreach($posts as $post){ setup_postdata($post);?>
            <a href="<?php the_permalink() ?>">
                <?php echo link_words(limit_words(get_field(\'content\'), $instance[\'words_to_display\']))."...";
                if($shows != ($instance[\'count_of_posts\']-1)) echo \'<hr>\';
                $shows++;
            }
            wp_reset_postdata();
        endif;

        echo "</div>";
    }
}

1 个回复
SO网友:Bulbul bigboss

在公共功能小部件中这样写

echo $args[\'before_widget\'] ;
echo $args[\'before_title\'] ;
//your tile should here
echo echo $args[\'after_title\'] ;
echo echo $args[\'after_widget\'] ;

相关推荐

My widgets do not save

每次我保存我的小部件并离开页面时,我的小部件都会消失。侧边栏已完全清空,不会保存任何更改。控制台或PHP日志中没有任何错误。如果我将小部件直接复制并保存在数据库中widgets_text, 它们将被显示,但我仍然无法在侧边栏中添加或删除任何内容。这只发生在我的右侧边栏上,左侧边栏工作正常,但它们都以相同的方式注册。这是我注册侧边栏的方式:function my_widgets_init() { register_sidebar( array ( \'name\'