我正在创建一个最近的帖子小部件练习,但我需要帮助正确地使它,以便管理员可以调整显示的类别



就像我在标题中说的,我正在创建一个最近的帖子小部件来练习。它包括不同的功能和一个,我有问题,它是允许管理员选择他们想要显示的类别。我目前有它的设置,以便在它的形式有一个文本字段中,管理员可以手动输入的类别,但我需要它是这样的,它有一个下拉菜单,已经有类别,所以管理员可以选择它。我不知道如何做到这一点,所以如果我能得到一些帮助,那将是伟大的。对不起,如果我是含糊的,我是新手。我把我的代码贴在下面。

<?php
/*
Plugin Name: News Recent Posts Widget
Plugin URI: 
Description: A recent post widget with extra functions that allow admin to make changes to certain values
Author: Kevin Ullyott
Version: 1.0
Author URI: http://modmacro.com/
*/

class recentpost extends WP_Widget {
    public function __construct() {
    parent::WP_Widget(
    // or parent::__construct(
        false, 
        'Kevin - Recent Posts Widget',
        array(
            'description' => __('A recent post widget with extra functions that allow admin to make changes to certain values') 
        )
    );
    ;
}
    public function widget( $args, $instance ) {

        extract( $args );
        $headline = $instance['headline'];
        $category = $instance['category'];
        $numberposts = $instance['numberposts'];
        $readmore = $instance['readmore'];

        echo $before_widget;
        echo $before_title;
        echo "<p class="headline">$headline</p>";
        echo $after_title;
        $args = array( 'numberposts' => $numberposts, 'category_name' => $category );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ){
        setup_postdata(get_post($recent['ID']));
        echo '<a href="' . get_permalink($recent['ID']) . '" title=" '.esc_attr(get_the_title($recent['ID'])).'" >' .   get_the_title($recent['ID']).'</a> ';
        echo get_the_time('F j, Y', $recent['ID']);
        the_excerpt();
}
wp_reset_postdata();

        echo $after_widget;

    }
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['headline'] = ( $new_instance['headline'] );
        $instance['category'] = ( $new_instance['category'] );
        $instance['numberposts'] = ( $new_instance['numberposts'] );
        $instance['readmore'] = ( $new_instance['readmore'] );
        return $instance;
    }

    public function form( $instance ) {
        $headline = $instance[ 'headline' ];
        $category = $instance[ 'category' ];
        $numberposts = $instance[ 'numberposts' ];
        $readmore = $instance[ 'readmore' ];
        ?>
<p>
  <label for="<?php echo $this->get_field_id( 'headline' ); ?>">
    <?php _e( 'Headline:' ); ?>
  </label>
  <input class="widefat" id="<?php echo $this->get_field_id( 'headline' ); ?>" name="<?php echo $this->get_field_name( 'headline' ); ?>" type="text" value="<?php echo esc_attr( $headline ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'category' ); ?>">
  <?php _e( 'Category:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>" type="text" value="<?php echo esc_attr( $category ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'numberposts' ); ?>">
  <?php _e( 'Number of posts:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'numberposts' ); ?>" name="<?php echo $this->get_field_name( 'numberposts' ); ?>" type="text" value="<?php echo esc_attr( $numberposts ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'readmore' ); ?>">
  <?php _e( 'Read More:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'readmore' ); ?>" name="<?php echo $this->get_field_name( 'readmore' ); ?>" type="text" value="<?php echo esc_attr( $readmore ); ?>" />
</p>
<?php 
    }
}
add_action( 'widgets_init', create_function('', 'return register_widget("recentpost");') );

?>

函数wp_dropdown_categories将引入类别,但单独添加函数不会保存或检索所选的类别。您需要添加nameselectedid参数。我想这应该行得通:

<?php wp_dropdown_categories(array('name' => $this->get_field_name('category'), 'selected' => $category, 'id' => $this->get_field_id('category'), 'class' => 'widefat')); ?>

我也添加了widefat类,但它只是为了表示。

函数wp_dropdown_categories显示一个下拉菜单,其中拉入站点中使用的所有类别。这里是它的文档:http://codex.wordpress.org/Function_Reference/wp_dropdown_categories

所以你可以替换

get_field_id('category');?>" name="get_field_name('category');?>" type="text" value="

使用调用wp_dropdown_categories函数

最新更新