Quantcast
Channel: Topic Tag: plugin | WordPress.org
Viewing all articles
Browse latest Browse all 26892

bronzehedwick on "Foreach loop inside widget update function?"

$
0
0

Thanks Chris!

You're right, $new_instance['post_types'][$post_type] did not exist. I hacked around a bit and revised my code and I think I made progress; however, when running the update function the values still aren't being saved for $instance['post_type']. Not sure why this is happening, as $instance['num_posts'] works fine.

// build the widget settings form
	function form( $instance ) {
		$defaults   = array( 'num_posts' => 5 );
		$instance   = wp_parse_args( (array) $instance, $defaults );
		$num_posts  = $instance['num_posts'];

    foreach ( get_post_types() as $post_type ) {
      $instance['post_types'] .= $post_type . ',';
    }
    $post_types = $instance['post_types'];
		//echo '<pre>';
    //print_r($instance);
		//echo '</pre>';

		?>

			<p>Number of posts to show: <input class="widefat" name="<?php echo $this->get_field_name( 'num_posts' ); ?>" type="number" value="<?php echo esc_attr( $num_posts ); ?>" /></p>
			<p>Filter by Post Type: <ul>
			<?php foreach ( get_post_types() as $post_type ) { ?>
				<li><input name="<?php echo $this->get_field_name( $post_type ); ?>" type="checkbox" <?php checked( $this->get_field_name( $post_type ) ); ?> /><label for="<?php echo $this->get_field_name( $post_type ); ?>" /><?php echo $post_type; ?></label></li>
			<?php
		   } ?>
			</ul></p>

		<?php
    echo '<pre>';
    print_r($instance);
    echo '</pre>';
	}

	// save the widget settings
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['num_posts'] = strip_tags( $new_instance['num_posts'] );
    $instance['post_types'] = strip_tags( $new_instance['post_types'] );

		return $instance;
	}

Viewing all articles
Browse latest Browse all 26892

Trending Articles