foreach在第一个值之后关闭,然后显示剩余的值

时间:2011-10-14 19:35:16

标签: php wordpress foreach widget

我写了一个wordpress小部件,列出了标签中博客的类别。最初我编写的代码可以工作,您可以选择一个值,当您单击保存时,小部件会在前端显示正确的信息。但是,在重新加载数组后点击保存后,它看起来像这样:

<select name="widget-cheer_recent_posts[7][cat]" id="widget-cheer_recent_posts-7-cat" class="widefat"></select>

<option value="72" id="Elite">Elite</option>
</p>
<option value="96" id="Featured">Featured</option>
<option value="70" id="Junior Varsity">Junior Varsity</option>
<option value="105" id="JV">JV</option>
<option value="67" id="Mixes">Mixes</option>
<option value="97" id="promo">promo</option>
<option value="99" id="Samples">Samples</option>
<option value="1" id="Uncategorized">Uncategorized</option>
<option value="71" id="Varsity">Varsity</option>

我不确定这是如何发生的,因为下拉列表第一次正确加载,只有在wordpress中保存和AJAX重新加载才会失败。这是小部件的代码:

define ('CHEER_PLUGIN_DIR', dirname( plugin_basename( __FILE__ ) ));
define ('CHEER_PLUGIN_URL', plugins_url() . '/' . CHEER_PLUGIN_DIR);

class cheer_recent_posts extends WP_Widget {


    /** constructor */
    function cheer_recent_posts() {
        parent::WP_Widget(false, $name = 'Cheer Samples');  
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) { 
        extract( $args );
        global $posttypes;

        global $cats;
        $title          = apply_filters('widget_title', $instance['title']);
        $cat            = $instance['cat'];
        $number         = apply_filters('widget_title', $instance['number']);
        $offset         = apply_filters('widget_title', $instance['offset']);
        $thumbnail_size = apply_filters('widget_title', $instance['thumbnail_size']);
        $thumbnail      = $instance['thumbnail'];
        $posttype       = $instance['posttype'];
        ?>
              <?php echo $before_widget; ?>
                  <?php if ( $title )
                        echo $before_title . $title . $after_title; ?>
                            <?php
                                global $post;
                                $tmp_post = $post;

                                // get the category IDs and place them in an array

                                $args = 'numberposts=' . $number . '&offset=' . $offset . '&post_type=' . $posttype . '&cat=' . $cat;
                                $myposts = get_posts( $args );
                                foreach( $myposts as $post ) : setup_postdata($post); ?>
                                    <div id="cs-background" style="border: 5px solid white; background:url(<?php echo (CHEER_PLUGIN_URL); ?>/timthumb/timthumb.php?src=<?php echo get_post_meta($post->ID, 'nitro_post_image', true) ?>&w=270&h=157&zc=1) no-repeat;">
                                        <div class="cs-audio">

                                        <audio style="width:270px; margin:115px 0 0 0;" controls="controls">

                                        <source src="<?php echo get_post_meta($post->ID, 'song-sample', true) ?>" type="audio/mp3" />
                                            <object type="application/x-shockwave-flash" data="<?php echo (CHEER_PLUGIN_URL); ?>/dewplayer/dewplayer-mini.swf" width="266" height="20" id="dewplayer" name="dewplayer"> 
                                                <param name="wmode" value="transparent" />
                                                <param name="movie" value="dewplayer-mini.swf" /> 
                                                <param name="flashvars" value="mp3=<?php echo get_post_meta($post->ID, 'song-sample', true) ?>" /> 
                                            </object>

                                        </audio>
                                        </div>
                                    </div>
                                    <div>
                                        <?php the_title( '<h2 id="mix-title" class="post-title entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>

                                    <!--    <h2 id="cs-title" class="post-title entry-title">
                                            <?php if($thumbnail == true) { ?>
                                                <?php the_post_thumbnail(array($thumbnail_size));?>
                                            <?php } ?>
                                                <?php the_title(); ?>
                                        </h2>
                                        <?php the_content(); ?>  -->
                                    </div>
                                <?php endforeach; ?>
                                <?php $post = $tmp_post; ?>
              <?php echo $after_widget; ?>
        <?php
    }
    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {     
        global $posttypes;
        global $cats;       
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['cat'] = $new_instance['cat'];
        $instance['number'] = strip_tags($new_instance['number']);
        $instance['offset'] = strip_tags($new_instance['offset']);
        $instance['thumbnail_size'] = strip_tags($new_instance['thumbnail_size']);
        $instance['thumbnail'] = $new_instance['thumbnail'];
        $instance['posttype'] = $new_instance['posttype'];
        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {  

        $posttypes = get_post_types('', 'objects');
        $cats = get_categories('hide_empty=0'); 

        $title = esc_attr($instance['title']);
        $cat = esc_attr($instance['cat']);        
        $number = esc_attr($instance['number']);
        $offset = esc_attr($instance['offset']);
        $thumbnail_size = esc_attr($instance['thumbnail_size']);
        $thumbnail = esc_attr($instance['thumbnail']);
        $posttype = esc_attr($instance['posttype']);
        ?>
         <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
        <p>
          <label for="<?php echo $this->get_field_id('cat'); ?>"><?php _e('Category you want to use'); ?></label> 
          <select name="<?php echo $this->get_field_name('cat'); ?>" id="<?php echo $this->get_field_id('cat'); ?>" class="widefat" />
                    <?php
                foreach ($cats as $catsoption) {
                    echo '<option value="' . $catsoption->cat_ID . '" id="' . $catsoption->name . '"', $cat == $catsoption->cat_ID ? ' selected="selected"' : '', '>', $catsoption->name, '</option>';
                }
                ?>
           </select>
        </p>
        <p>
          <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number to Show:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
        </p>
        <p>
          <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Offset (the number of posts to skip):'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('offset'); ?>" name="<?php echo $this->get_field_name('offset'); ?>" type="text" value="<?php echo $offset; ?>" />
        </p>
        <p>
          <input id="<?php echo $this->get_field_id('thumbnail'); ?>" name="<?php echo $this->get_field_name('thumbnail'); ?>" type="checkbox" value="1" <?php checked( '1', $thumbnail ); ?>/>
          <label for="<?php echo $this->get_field_id('thumbnail'); ?>"><?php _e('Display thumbnails?'); ?></label> 
        </p>
        <p>
          <label for="<?php echo $this->get_field_id('thumbnail_size'); ?>"><?php _e('Size of the thumbnails, e.g. <em>80</em> = 80px x 80px'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('thumbnail_size'); ?>" name="<?php echo $this->get_field_name('thumbnail_size'); ?>" type="text" value="<?php echo $thumbnail_size; ?>" />
        </p>
        <p> 
            <label for="<?php echo $this->get_field_id('posttype'); ?>"><?php _e('Choose the Post Type to display'); ?></label> 
            <select name="<?php echo $this->get_field_name('posttype'); ?>" id="<?php echo $this->get_field_id('posttype'); ?>" class="widefat">
                <?php
                foreach ($posttypes as $option) {
                    echo '<option value="' . $option->name . '" id="' . $option->name . '"', $posttype == $option->name ? ' selected="selected"' : '', '>', $option->name, '</option>';
                }
                ?>
            </select>       
        </p>
        <?php 
    }


} // class utopian_recent_posts
// register Recent Posts widget
add_action('widgets_init', create_function('', 'return register_widget("cheer_recent_posts");'));

0 个答案:

没有答案
相关问题