$instance variable checking null difficulty
-
Hello, I am currently developing a widget plugin and I am getting an input from user and that input is stored in
$instance['code']
I would like to use this as argument to restrict the number of posts shown on the main website. However, if the input is not provided, then I want to set the default as 10 (so display 10 posts).
It worked fine when I used form as I use isset function to determine whether the input was null but now, in widget function that does not work and I want to know how can I achieve this.
Here is part of my code (I tested that the variable behaves correctly!)
public function widget($args, $instance) { extract($args, EXTR_SKIP); //global WP theme-driven "before widget" code echo $before_widget; // code before your user input //echo '<iframe width="560" height="315" src="//www.youtube.com/embed/'; //echo $instance['code']; // code after your user input //echo '"frameborder="0" allowfullscreen></iframe>'; if($instance['code'] == null){ $numPosts = 10; }else{ $numPosts = $instance['code']; } $cateName = $instance['title']; $category_id = get_cat_ID($cateName); $catquery = new WP_Query( 'cat=' .$category_id); if($catquery->have_posts()): while($numPosts > 0 && $catquery->have_posts()) : $catquery->the_post(); $numPosts -= 1;?> <article class="post"> <h2> <a href="<?php the_permalink(); ?>"> <?php the_title();?></a> </h2> <?php the_post_thumbnail(array(20,20)); ?> written by <?php the_author(); ?> <h3 id="comments"> <?php comments_number('no responses', 'one response', '% responses'); ?> </h3> <?php the_content(); ?> </article> <?php endwhile; else : echo '<p> No content found </p>'; endif; wp_reset_query(); f//global WP theme-driven "after widget" code echo $after_widget; }
if($instance['code'] == null){ $numPosts = 10; }else{ $numPosts = $instance['code']; }
This part is the intention but does not work. Can anyone help?
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘$instance variable checking null difficulty’ is closed to new replies.