向自定义小部件添加其他控件时需要帮助

时间:2017-05-03 作者:The WP Intermediate

public function widget($args, $instance) {
    extract( $args );
    $title = apply_filters( \'widget_title\', $instance[\'title\'] );

    /* Display the markup before the widget. */
    echo $before_widget;
    if ( $title ) {
        echo $before_title . $title . $after_title;
    }
    /* Create a custom query and get the most recent 4 projects. */
    $queryArgs = array(
        /* Do not get posts from the Uncategorized category. */
        \'cat\' => \'-1\',
        /* Order by date. */
        \'orderby\' => \'date\',
        /* Show all posts. */
        \'posts_per_page\' => \'4\'
    );
    $query = new WP_Query( $queryArgs );
    if ( $query->have_posts() ) : ?>
        <ul class="unbullet unbullet-v">
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <li class="snippet-box vertical">
                <div>
                    <!-- <img src="http://heightandweights.com/wp-content/uploads/2014/10/Beautiful-Lindsey-Vonn.jpg" alt="" class="hundred"> -->
                    <?php the_post_thumbnail( \'medium\', array( \'class\' => \'hundred\' ) ); ?>
                    <div class="snippet-text">
                        <h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
                    </div>
                </div>
                <!-- <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( \'large\', array( \'class\' => \'img-responsive\' ) ); ?></a> -->
            </li>
        <?php endwhile; ?>
        </ul>
    <?php endif;
    /* Display the markup after the widget. */
    echo $after_widget;
}
以上是我的小部件功能。我需要一些帮助。

Issue #1 →

            \'posts_per_page\' => \'4\'
我希望这是动态的,这意味着在后端小部件表单中应该有一个输入框,管理员应该能够输入要显示的帖子数量。

我试过这样→这是后端中的Form方法→`

public function form( $instance ) {
    /* Defaults. */
    $defaults = array(
        \'title\' => \'Recent Projects\',
    );

    $instance = wp_parse_args( (array) $instance, $defaults ); ?>
    <p>
        <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\', \'tuts\' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo esc_attr( $instance[\'title\'] ); ?>">
    </p>

    <?php
}
我是这样操纵这个表格的→ (但它不起作用,产生了错误)

public function form( $instance ) {
    /* Defaults. */
    $defaults = array(
        \'title\' => \'Recent Projects\',
        \'post-number\'  =>\'4\';
    );

    $instance = wp_parse_args( (array) $instance, $defaults ); ?>
    <p>
        <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\', \'tuts\' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo esc_attr( $instance[\'title\'] ); ?>">
    </p>

    <p>
        <label for="<?php echo $this->get_field_id( \'post-number\' ); ?>"><?php _e( \'Post :\', \'tuts\' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id( \'post-number\' ); ?>" name="<?php echo $this->get_field_name( \'post-number\' ); ?>" value="<?php echo esc_attr( $instance[\'post-number\'] ); ?>">
    </p>

    <?php
}
小结:我想要一个后端表单输入,用于在小部件中显示的帖子数量,然后在小部件方法中反映相同数量的帖子,以便可以在前端显示所选的帖子。

Issue #2 →在小部件表单方法中,您可以看到→<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>

我希望这是动态的,这是可能的→

<p><?php the_content(); ?></p>
但这将公布整个帖子内容,这是不可取的。我想就像应该有一个输入表单方法一样,从帖子内容中提取的字符数应该由表单输入框中输入的数值决定,并且小部件前端应该显示相同数量的字符。

THE PART 2 OF THIS QUESTION IS HERE

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

下面是基于您的代码的项目小部件的完整示例。此小部件将允许用户指定要显示的项目帖子数量,并允许对内容执行字数限制。

请注意,我对内容使用了字数限制,而不是字符限制。这是因为一旦涉及HTML和短代码,字符限制就会变得有点危险。

作为旁白,我发现看看core\'s widgets 作为构建自定义小部件的参考。核心团队非常擅长为新版本的WordPress更新小部件,因此源代码是一个很好的参考。

class WPSE_Projects_Widget extends WP_Widget {

    /**
     * Register widget with WordPress.
     */
    function __construct() {
        parent::__construct(
            \'wpse_projects_widget\', // Base ID
            esc_html__( \'WPSE Projects Widget\', \'text_domain\' ), // Name
            array( \'description\' => esc_html__( \'Display some projects.\', \'text_domain\' ), ) // Args
        );
    }

    /**
     * Outputs the content of the widget
     *
     * @param array $args
     * @param array $instance
     */
public function widget($args, $instance) {

    echo $args[\'before_widget\'];
    if ( ! empty( $instance[\'title\'] ) ) {
        echo $args[\'before_title\'] . apply_filters( \'widget_title\', $instance[\'title\'] ) . $args[\'after_title\'];
    }

    $number_of_posts = ( ! empty( $instance[\'number_of_posts\'] ) ) ? absint( $instance[\'number_of_posts\'] ) : 5;
    if ( ! $number_of_posts ) {
        $number_of_posts = 5;
    }


    $number_of_words = ( ! empty( $instance[\'number_of_words\'] ) ) ? absint( $instance[\'number_of_words\'] ) : 20;
    if ( ! $number_of_words ) {
        $number_of_words = 20;
    }

    /* Create a custom query and get the most recent 4 projects. */
    $query_args = array(
            /* Do not get posts from the Uncategorized category. */
            \'cat\' => \'-1\',
            /* Order by date. */
            \'orderby\' => \'date\',
            /* Number of posts to get. */
            \'posts_per_page\' => $number_of_posts,
    );
    $query = new WP_Query( $query_args );
    if ( $query->have_posts() ) : ?>
            <ul class="unbullet unbullet-v">
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                    <li class="snippet-box vertical">
                            <div>
                                    <!-- <img src="http://heightandweights.com/wp-content/uploads/2014/10/Beautiful-Lindsey-Vonn.jpg" alt="" class="hundred"> -->
                                    <?php the_post_thumbnail( \'medium\', array( \'class\' => \'hundred\' ) ); ?>
                                    <div class="snippet-text">
                                            <h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                                            <?php echo wp_trim_words( get_the_excerpt(), $number_of_words, __( \'&hellip;\', \'text_domain\' ) ); ?>
                                    </div>
                            </div>
                            <!-- <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( \'large\', array( \'class\' => \'img-responsive\' ) ); ?></a> -->
                    </li>
            <?php endwhile; ?>
            </ul>
    <?php endif;

    echo $args[\'after_widget\'];
}

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form( $instance ) {
        $title = isset( $instance[\'title\'] ) ? $instance[\'title\'] : \'\';
        $number_of_posts = isset( $instance[\'number_of_posts\'] ) ? absint( $instance[\'number_of_posts\'] ) : 5;
        $number_of_words = isset( $instance[\'number_of_words\'] ) ? absint( $instance[\'number_of_words\'] ) : 20;
        ?>
        <p><label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\', \'text_domain\' ); ?></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>

        <p><label for="<?php echo $this->get_field_id( \'number_of_posts\' ); ?>"><?php _e( \'Number of projects to show:\', \'text_domain\' ); ?></label>
        <input class="tiny-text" id="<?php echo $this->get_field_id( \'number_of_posts\' ); ?>" name="<?php echo $this->get_field_name( \'number_of_posts\' ); ?>" type="number" step="1" min="1" value="<?php echo $number_of_posts; ?>" size="3" /></p>

        <p><label for="<?php echo $this->get_field_id( \'number_of_words\' ); ?>"><?php _e( \'Set the word limit for project descriptions:\', \'text_domain\' ); ?></label>
        <input class="tiny-text" id="<?php echo $this->get_field_id( \'number_of_words\' ); ?>" name="<?php echo $this->get_field_name( \'number_of_words\' ); ?>" type="number" step="1" min="1" value="<?php echo $number_of_words; ?>" size="4" /></p>       
        <?php
    }

    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance[\'title\']           = sanitize_text_field( $new_instance[\'title\'] );
        $instance[\'number_of_posts\'] = absint( $new_instance[\'number_of_posts\'] );
        $instance[\'number_of_words\'] = absint( $new_instance[\'number_of_words\'] );
        return $instance;
    }
}
注册小部件:
function wpse_register_widget() {
    register_widget( \'WPSE_Projects_Widget\' );
}
add_action( \'widgets_init\', \'wpse_register_widget\' );

相关推荐

My widgets do not save

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