0

所以就像我在标题中所说的那样,我正在创建一个最近的帖子小部件以供练习。它包括不同的功能,我遇到的问题是允许管理员选择他们想要显示的类别。我目前已对其进行设置,以便在表单中它有一个文本字段,管理员可以在其中手动输入类别,但我需要它有一个下拉菜单,其中已经有类别,所以管理员可以只需选择它。我根本不知道该怎么做,所以如果我能得到一些帮助,那就太好了。对不起,如果我含糊其辞,我是新手。我在下面发布我的代码。

<?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");') );


?>
4

2 回答 2

1

函数 wp_dropdown_categories 将引入类别,但单独添加函数不会保存或检索所选类别。您需要添加name selectedandid参数。我认为这应该可行:

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

我也添加了课程widefat,但它仅用于演示。希望有帮助。

于 2013-01-07T05:36:50.323 回答
0

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

所以你可以更换

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

调用 wp_dropdown_categories 函数。

于 2013-01-07T04:51:51.630 回答