WordPress小工具开发

标准

收集一些小工具的代码

分类文章

显示当前文章分类下的文章

/**
 * My_Theme_Classified_Articles_Widget Class
 */
class My_Theme_Classified_Articles_Widget extends WP_Widget
{
    /**
     * Constructor
     *
     * My_Theme_Classified_Articles_Widget constructor.
     */
    public function __construct()
    {
        parent::__construct(
            'widget_mytheme_classified_articles', __('Theme Classified Articles'), array(
                'classname' => 'widget_mytheme_classified_articles',
                'description' => __('Show articles under a single category.'),
                'customize_selective_refresh' => true,
            )
        );
    }

    /**
     * Output the HTML for this widget.
     *
     * @param array $args
     * @param array $instance
     */
    public function widget($args, $instance)
    {
        global $post;

        if (is_single()) { //文档
            $current_id = get_the_ID();
            $categories = get_the_category();

            if ($categories) {
                echo $args['before_widget'];

                ?>
                <h1 class="widget-title"><?php echo $categories[0]->name; ?></h1>
                <ul>
                    <?php
                    $posts = get_posts(array(
                        'numberposts' => $instance['number'],
                        'category' => $categories[0]->term_id,
                        'order' => 'ASC',
                    ));
                    foreach ($posts as $k => $_post) {
                        ?>
                        <li>
                            <a href="<?php echo get_permalink($_post->ID); ?>"
                               class="<?php if ($_post->ID == $current_id) {
                                   echo 'active';
                               } ?>">
                                <?php echo ++$k . '. ' . get_the_title($_post->ID); ?>
                            </a>
                        </li>
                        <?php
                    }
                    ?>
                    <li style="margin-top: 10px"><a href="<?php echo get_category_link($categories[0]->term_id); ?>">查看更多>></a>
                    </li>
                </ul>
                <?php

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

    /**
     * Deal with the settings when they are saved by the admin.
     *
     * Here is where any validation should happen.
     *
     * @param array $new_instance
     * @param array $old_instance
     * @return array
     */
    public function update($new_instance, $instance)
    {
        $instance['number'] = strip_tags($new_instance['number']);
        return $instance;
    }

    /**
     * Display the form for this widget on the Widgets page of the Admin area.
     *
     * @param array $instance
     */
    public function form($instance)
    {
        $number = isset($instance['number']) ? absint($instance['number']) : 5;
        ?>
        <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
            <input class="tiny-text" id="<?php echo $this->get_field_id('number'); ?>"
                   name="<?php echo $this->get_field_name('number'); ?>" type="number" step="1" min="1"
                   value="<?php echo $number; ?>" size="3"/>
        </p>
        <?php
    }

} // class My_Theme_Classified_Articles_Widget
register_widget('My_Theme_Classified_Articles_Widget');

文章作者

显示当前文章的作者资料

/**
 * Author_Profile_Widget Class
 */
class Author_Profile_Widget extends WP_Widget
{
    /**
     * Constructor
     *
     * Author_Profile_Widget constructor.
     */
    public function __construct()
    {
        parent::__construct(
            'widget_author_profile', __('Author Profile'), array(
                'classname' => 'widget_author_profile',
                'description' => __('Show Author Profile.'),
                'customize_selective_refresh' => true,
            )
        );
    }

    /**
     * Output the HTML for this widget.
     *
     * @param array $args
     * @param array $instance
     */
    public function widget($args, $instance)
    {
        global $post;

        if (is_single() && $post->post_type == 'post') { //文档
            echo $args['before_widget'];
            ?>
            <div class="author-bio" style="border-color:#fff;margin-bottom: 0;">
                <div class="bio-avatar"><?php echo get_avatar( get_the_author_meta('user_email'),'128' ); ?></div>
                <p class="bio-name"><?php the_author_posts_link(); ?></p>
                <p class="bio-desc"><?php the_author_meta('description'); ?></p>
                <div class="clear"></div>
           </div>
            <?php
            echo $args['after_widget'];
        }        
    }

} // class Author_Profile_Widget
register_widget('Author_Profile_Widget');

发表评论

电子邮件地址不会被公开。 必填项已用*标注

− 9 = 1