0

I am currently being trained to create widgets and my mentor has asked me to create a text widget then add the ability to display the 5 most recent posts (all categories) on it. I am a little stumped on how to add it in as I am very new to this. I will post the code below along with the url. What I was recommended to use is Function Reference/wp get recent posts from the codex but I am stumped on how to get it to work.

<?php
/*
Plugin Name: Kevins Textbox
Plugin URI: 
Description: A text widget created by Kevin Ullyott for practice in creating widgets
Author: Kevin Ullyott
Version: 1.0
Author URI:
*/


class kevintext extends WP_Widget {

    public function __construct() {
    parent::WP_Widget(
    // or parent::__construct(
        false, 
        'Kevins Textbox',
        array(
            'description' => __('A text widget created by Kevin Ullyott for practice in creating widgets') 
        )
    );
    ;
}

    public function widget( $args, $instance ) {


        extract( $args );

        $headline = $instance['headline'];      
        $text = $instance['text'];      



        echo $before_widget;

        echo $before_title;

        echo "<p class=\"headline\">$headline</p>";

        echo $after_title;

        echo "<p class=\"text\">$text</p>";

        echo $after_widget;


    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['headline'] = ( $new_instance['headline'] );
        $instance['text'] = ( $new_instance['text'] );
        return $instance;
    }


    public function form( $instance ) {

        $headline = $instance[ 'headline' ];
        $text = $instance[ 'text' ];

        ?>
        <p>
        <label for="<?php echo $this->get_field_id( 'headline' ); ?>"><?php _e( 'Headline:' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id( 'headline' ); ?>" name="<?php echo $this->get_field_name( 'headline' ); ?>" type="text" value="<?php echo esc_attr( $headline ); ?>" />
        </p>

        <p>
        <label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Text:' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" type="text" value="<?php echo esc_attr( $text ); ?>" />
        </p>


        <?php 
    }
}

add_action( 'widgets_init', create_function('', 'return register_widget("kevintext");') );


?>

Link to url: http://www.modmacro.us/wpsandbox/

Link to codex function: http://codex.wordpress.org/Function_Reference/wp_get_recent_posts

2
  • What has you stumped? I don't see any attempt to get or display recent posts. What have you tried?
    – s_ha_dum
    Commented Dec 16, 2012 at 21:37
  • I am just not sure how to add it in at all to be honest. Commented Dec 16, 2012 at 23:20

1 Answer 1

1

In the function widget( $args, $instance ), which generates the widget output, add the following code before the echo $after_widget; line:

// The Query
$the_query = new WP_Query( array( 'post_type' => 'page', 'posts_per_page' => '3' ) );

// The Loop
while ( $the_query->have_posts() ) :
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
endwhile;

Additional information can be found on the WordPress Codex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.