$instance variable checking null difficulty (2 posts)

  1. jaehee0113
    Posted 1 year ago #

    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;
    			$numPosts = $instance['code'];
    		$cateName = $instance['title'];
    		$category_id = get_cat_ID($cateName);
    		$catquery = new WP_Query( 'cat=' .$category_id);
    			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(); ?>
    			<?php endwhile;
    		else :
    			echo '<p> No content found </p>';
    		f//global WP theme-driven "after widget" code
    		echo $after_widget;
    if($instance['code'] == null){
    			$numPosts = 10;
    			$numPosts = $instance['code'];

    This part is the intention but does not work. Can anyone help?

  2. jaehee0113
    Posted 1 year ago #

    Sorry there was a type there it works perfectly now :)

Topic Closed

This topic has been closed to new replies.

About this Topic