練習用にさまざまな機能を備えたWordpressの最近の投稿ウィジェットを作成しています。私が実行する必要がある機能の 1 つは、管理者がカテゴリを含むドロップダウン メニューを介して表示する 1 つのカテゴリを指定できるようにすることでした。ここでこれについて助けを求めて答えを得ましたが、正しく機能させる方法がわかりません。これが私のコードです:
<?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>
<?php wp_dropdown_categories(array('name' => $this->get_field_name('category'), 'selected' => $category, 'id' => $this->get_field_id('category'), 'class' => 'widefat')); ?>
</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");') );
?>
カテゴリのフォームを見てみると、そこに PHP コードを挿入してドロップダウン メニューを作成しています。フォームが正常に作成され、ドロップダウン メニュー内に正しいカテゴリが表示されますが、保存すると正しく更新されず、ウィジェットに何も表示されません。
このコードをくれたユーザーが教えてくれました。彼は次のように述べています。しかし問題は、name、selected、および id 引数を追加する方法がわからないことです。私がやろうとしたことは、これまでのところすべて失敗しています。私のコードを使用してこれを行う正しい方法を教えてください。それは大いに役立つでしょう。私はこれに非常に慣れておらず、学ぼうとしているので、私がする必要があることと、それを説明できるかどうかを教えてください.
コーディングが好きで、できるだけ多くのことを知りたいと思っています。