我正在尝试构建一个wordpress小部件,它会列出小部件的类别和输入字段,小部件中的帖子数量完美, 但问题是当我试图从下拉类别部分获取值到我的代码将输出选定的类别帖子时,我没有得到类别ID,为了测试目的,我添加了这两行:
echo $catno; /*tested got right output as 5*/
echo $catid; /*tested but no output*/
我正在制作和错误我不知道 这是我的完整小部件代码:
class category_one extends WP_Widget {
// constructor
function category_one() {
parent::WP_Widget(false, $name = __('Category One', 'wp_widget_plugin') );
}
// widget form creation
function form($instance) {
// Check values
if( $instance) {
$catid = $instance['catid'];
$catno = esc_attr($instance['catno']);
} else {
$catid = '';
$catno = '';
}
?>
<p>
<label for="<?php echo $this->get_field_id('catid'); ?>"><?php _e('Select Category', 'wp_widget_plugin'); ?></label>
<?php wp_dropdown_categories( array( 'name' => $this->get_field_name("catid"), 'selected' => $instance["catid"] ) ); ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('catno'); ?>"><?php _e('No Posts', 'wp_widget_plugin'); ?></label>
<input class="catone" id="<?php echo $this->get_field_id('catno'); ?>" name="<?php echo $this->get_field_name('catno'); ?>" type="text" value="<?php echo $catno; ?>" />
</p>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance['catid'] = $new_instance['catid'];
$instance['catno'] = $new_instance['catno'];
return $instance;
}
// display widget
function widget($args, $instance) {
extract( $args );
// these are the widget options
$catid = $instance['catid'];
$catno = $instance['catno'];
echo $before_widget;
echo $catno; /*tested got right output*/
echo $catid; /*tested but no output got right output*/
global $post;
if (!$catid) { }
else {
$myposts = get_posts(array('numberposts' => $catno, 'offset' => 0, 'category__in' => array($catid), 'post_status'=>'publish', 'order'=>'DEC' ));
echo '<div class="catlist">';
echo '<h2 class="sidebar_title"><a href="' . get_category_link($catid) . '" title="'. get_cat_name($catid) .'">' . get_cat_name($catid) . '</a></h4>';
echo "<ul class='widget_catlist'>";
foreach($myposts as $post) :
setup_postdata($post);
echo '<li><a href="' . get_permalink() . '" title="'. get_the_title() .'">' . get_the_title() . '</a></li>';
endforeach;
wp_reset_query();
echo "</ul>";
echo '</div>';
}
echo $after_widget;
}
}
// register widget
add_action('widgets_init', create_function('', 'return register_widget("category_one");'));
?>