Here’s the code of my widget
class gkd_img extends WP_Widget {
function __construct() {
parent::__construct('gkd_img', $name = __('GKD Featured Images'));
}
public function widget($args, $instance) {
echo '<div id="djnow" class="pWidget2">';
echo $args['before_widget'];
$n = $instance['num'];
$post = $instance['page'];
$page = get_permalink($post);
unset($images);
$images = get_children( array (
'post_parent' => $post,
'post_type' => 'attachment',
'posts_per_page' => $n,
'post_mime_type' => 'image'
));
//print_r($images);
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
?>
<a href="<?php echo $page ?>" class="pImages"> <?php echo wp_get_attachment_image( $attachment_id, 'post-thumb' ); ?> </a>
<?php
}
}
echo $args['after_widget'];
echo '</div>';
}
public function update($new_instance, $old_instance) {
$instance['num'] = $new_instance['num'];
$instance['page'] = strip_tags( $new_instance['page'] );
return $instance;
}
public function form($instance) {
$num;
if (isset($instance['num'])) {
$num = $instance['num'];
}
echo '<p><label for="'.$this->get_field_id('num').'">Number of images:</label><br>';
?>
<select id="<?php echo $this->get_field_id( 'num' ); ?>" name="<?php echo $this->get_field_name( 'num' ); ?>" type="text">
<option value="0" <?php echo "0" == $num ? "selected" : ""; ?>>0</option>
<option value="1" <?php echo "1" == $num ? "selected" : ""; ?>>1</option>
<option value="2" <?php echo "2" == $num ? "selected" : ""; ?>>2</option>
<option value="3" <?php echo "3" == $num ? "selected" : ""; ?>>3</option>
<option value="4" <?php echo "4" == $num ? "selected" : ""; ?>>4</option>
<option value="5" <?php echo "5" == $num ? "selected" : ""; ?>>5</option>
<option value="6" <?php echo "6" == $num ? "selected" : ""; ?>>6</option>
<option value="7" <?php echo "7" == $num ? "selected" : ""; ?>>7</option>
<option value="8" <?php echo "8" == $num ? "selected" : ""; ?>>8</option>
<option value="9" <?php echo "9" == $num ? "selected" : ""; ?>>9</option>
</select>
<?php
$defaults = array('page' => __('Select the page', 'example'));
$instance = wp_parse_args( (array) $instance, $defaults);
echo '<p><label for="'.$this->get_field_id('page').'">Select the page:</label><br>';
wp_dropdown_pages(array(
'id' => $this->get_field_id('page'),
'name' => $this->get_field_name('page'),
'selected' => $instance['page']
));
}
}
function gkd_img() {
register_widget( 'gkd_img' );
}
add_action( 'widgets_init', 'gkd_img' );
wp_enqueue_style( 'gkd_img', plugins_url('style.css', __FILE__) );
It is suposed to gfet images from the page user selects and show them on the homepage. But it allways shows the same ones. Its like the $images array was set once and it’s never refreshed.
Here is the link to the page:
https://casatagomago.com/WPweb/inicio/?language=es
Help please!!