2

为了对默认值应用一些更改,Recent Posts Widget我已经复制并修改了它以满足我的需要。它包含并注册在我的模板中functions.php,当在管理面板中加载小部件部分时,它的构造函数运行。

问题是它没有出现在可用小部件列表中,因此无法使用。当我尝试来自http://www.darrenhoyt.com/2009/12/22/creating-custom-wordpress-widgets/的最小示例时也会出现问题,所以我想我错过了一些重要的东西,但不知道是什么.

  • 到目前为止,我的主题是https://wordpress.org/extend/themes/toolbox的修改克隆。这解释了代码中多次出现的toolbox_
  • functions.php以下是和 的代码摘录inc/widgets.php。如果需要,我可以提供更多代码。请在评论中告诉我。

代码:

<?php

// BEGIN functions.php

if ( ! function_exists( 'toolbox_setup' ) ):
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which runs
 * before the init hook. The init hook is too late for some features, such as indicating
 * support post thumbnails.
 *
 * To override toolbox_setup() in a child theme, add your own toolbox_setup to your child theme's
 * functions.php file.
 */
function toolbox_setup() {
    // [...]

    require( get_template_directory() . '/inc/widgets.php' );

    // [...]
}
endif; // toolbox_setup

/**
 * Tell WordPress to run toolbox_setup() when the 'after_setup_theme' hook is run.
 */
add_action( 'after_setup_theme', 'toolbox_setup' );

/**
 * Register widgetized area and update sidebar with default widgets
 */
function toolbox_widgets_init() {

  register_widget("My_Widget_Recent_Posts");

    register_sidebar( array(
        'name' => __( 'Sidebar 1', 'toolbox' ),
        'id' => 'sidebar-1',
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => "</aside>",
        'before_title' => '<h1 class="widget-title small">',
        'after_title' => '</h1>',
    ) );
}
add_action( 'init', 'toolbox_widgets_init' );

// END functions.php

?>
<?php

// BEGIN inc/widgets.php

/**
 * Recent_Posts widget class
 *
 * @since 2.8.0
 */

class My_Widget_Recent_Posts extends WP_Widget {

  function __construct() {
        $widget_ops = array('classname' => 'my_widget_recent_entries', 'description' => __( "The most recent posts on your site (modified)") );
        parent::__construct('my-recent-posts', __('Recent Posts (modified)'), $widget_ops);
        $this->alt_option_name = 'my_widget_recent_entries';

        add_action( 'save_post', array(&$this, 'flush_widget_cache') );
        add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
        add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
    }

    function widget($args, $instance) {
        $cache = wp_cache_get('my_widget_recent_posts', 'widget');

        if ( !is_array($cache) )
            $cache = array();

        if ( ! isset( $args['widget_id'] ) )
            $args['widget_id'] = $this->id;

        if ( isset( $cache[ $args['widget_id'] ] ) ) {
            echo $cache[ $args['widget_id'] ];
            return;
        }

        ob_start();
        extract($args);

        $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
        if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
             $number = 10;

        $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true ) ) );
        if ($r->have_posts()) :
?>
        <?php echo $before_widget; ?>
        <?php if ( $title ) echo $before_title . $title . $after_title; ?>
        <ul>
        <?php  while ($r->have_posts()) : $r->the_post(); ?>
        <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a>
        <?php the_content(); ?></li>
        <?php endwhile; ?>
        </ul>
        <?php echo $after_widget; ?>
<?php
        // Reset the global $the_post as this query will have stomped on it
        wp_reset_postdata();

        endif;

        $cache[$args['widget_id']] = ob_get_flush();
        wp_cache_set('my_widget_recent_posts', $cache, 'widget');
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['number'] = (int) $new_instance['number'];
        $this->flush_widget_cache();

        $alloptions = wp_cache_get( 'alloptions', 'options' );
        if ( isset($alloptions['my_widget_recent_entries']) )
            delete_option('my_widget_recent_entries');

        return $instance;
    }

    function flush_widget_cache() {
        wp_cache_delete('my_widget_recent_posts', 'widget');
    }

    function form( $instance ) {
        $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
        $number = isset($instance['number']) ? absint($instance['number']) : 5;
?>
        <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 $title; ?>" /></p>

        <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
        <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<?php
    }
}

// END inc/widgets.php

?>
4

3 回答 3

5

你必须挂钩register_widgetwidgets_init行动,而不是init行动。在您的情况下,它是这样的:

<?php
add_action( 'widgets_init', 'toolbox_widgets_init' );

function toolbox_widgets_init() {
  register_widget("My_Widget_Recent_Posts");
}
?>
于 2014-06-05T14:27:00.190 回答
1

好的,让我们做一些调试测试:

echo "include widget file";
require( get_template_directory() . '/inc/widgets.php' );

还:

echo "register widget";
register_widget("My_Widget_Recent_Posts");

然后:

function __construct() {
echo "widget class";

检查您的 HTML 以查看哪些部分已回显,哪些部分未回显,您可能会收到标头已发送错误,您可以忽略该错误。希望我们可以看到哪个部分没有被触发以缩小问题所在。

于 2012-12-12T20:47:52.370 回答
1

如下注册小部件为我解决了这个问题,但在我看来不是很优雅。在类定义后面我插入了行

<?php
add_action( 'widgets_init', create_function('',
    'return register_widget("My_Widget_Recent_Posts");') );
?>

toolbox_widgets_init()并从我认为它所属的地方删除了注册。

万一有人可以解释为什么它必须这样,或者我可以改变什么,以便它像我打算做的那样工作,然后我会非常感谢评论。但是现在实际问题已经解决了。

于 2012-12-12T21:35:24.823 回答