Results 1 to 5 of 5

Thread: How to exclude most recent posts from recent posts widget

  1. #1
    MikeCloutier is offline Hello World
    Join Date
    Mar 2010
    Posts
    31

    Default How to exclude most recent posts from recent posts widget

    Since I've been having such great luck with advice which has helped me charge up my website, I have another question.

    I'd like to find a way to configure the Recent Posts widget to exclude a number of the most recent posts of my choosing. I'm sure it's an easy thing to do. I've looked around and downloaded some promising plugins but they don't seem to have the option to exclude a set number.

    The thing is, my home page is very deep with 10 posts (all split with the more tag) with three or four posts immediately viewable without scrolling, and I'd like to have a way for readers to see the list that is off their screens but on the page.

    A double bonus would be to exclude the posts that I have ordered as priority posts using a plugin called AStickyPostOrderEr even though their posting dates are older.

    Thanks again in advance.

  2. #2
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,992

    Default

    The recent comments widget uses wp_get_archives(), which, unfortunately, does not have an offset argument. Here are two potential solutions.
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  3. #3
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    Here you go. This is a modified version of the Recent posts widget, with an added Offset field. It's suitable for tossing directly into a plugin or theme's functions.php file.

    I only wrote this up in a minute or two, so there may be minor errors or bits I missed. Untested.

    PHP Code:
    <?php 
    /**
     * Recent_Posts widget class (modified to have an offset by Otto)
     */
    class WP_Widget_Recent_Posts_Offset extends WP_Widget {

        function 
    WP_Widget_Recent_Posts() {
            
    $widget_ops = array('classname' => 'widget_recent_entries''description' => __"The most recent posts on your blog") );
            
    $this->WP_Widget('recent-posts'__('Recent Posts'), $widget_ops);
            
    $this->alt_option_name 'widget_recent_entries';

            
    add_action'save_post', array(&$this'flush_widget_cache') );
            
    add_action'deleted_post', array(&$this'flush_widget_cache') );
            
    add_action'switch_theme', array(&$this'flush_widget_cache') );
        }

        function 
    widget($args$instance) {
            
    $cache wp_cache_get('widget_recent_posts''widget');

            if ( !
    is_array($cache) )
                
    $cache = array();

            if ( isset(
    $cache[$args['widget_id']]) ) {
                echo 
    $cache[$args['widget_id']];
                return;
            }

            
    ob_start();
            
    extract($args);

            
    $title apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance$this->id_base);
            if ( !
    $number = (int) $instance['number'] )
                
    $number 10;
            else if ( 
    $number )
                
    $number 1;
            else if ( 
    $number 15 )
                
    $number 15;

            
    $r = new WP_Query(array('showposts' => $number'nopaging' => 0'post_status' => 'publish''caller_get_posts' => 1'offset' => $offset));
            if (
    $r->have_posts()) :
    ?>
            <?php echo $before_widget?>
            <?php if ( $title ) echo $before_title $title $after_title?>
            <ul>
            <?php  while ($r->have_posts()) : $r->the_post(); ?>
            <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
            <?php endwhile; ?>
            </ul>
            <?php echo $after_widget?>
    <?php
                wp_reset_query
    ();  // Restore global post data stomped by the_post().
            
    endif;

            
    $cache[$args['widget_id']] = ob_get_flush();
            
    wp_cache_add('widget_recent_posts'$cache'widget');
        }

        function 
    update$new_instance$old_instance ) {
            
    $instance $old_instance;
            
    $instance['title'] = strip_tags($new_instance['title']);
            
    $instance['number'] = (int) $new_instance['number'];
            
    $instance['offset'] = (int) $new_instance['offset'];
            
    $this->flush_widget_cache();

            
    $alloptions wp_cache_get'alloptions''options' );
            if ( isset(
    $alloptions['widget_recent_entries']) )
                
    delete_option('widget_recent_entries');

            return 
    $instance;
        }

        function 
    flush_widget_cache() {
            
    wp_cache_delete('widget_recent_posts''widget');
        }

        function 
    form$instance ) {
            
    $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
            if ( !isset(
    $instance['number']) || !$number = (int) $instance['number'] )
                
    $number 5;
            if ( !isset(
    $instance['offset']) || !$number = (int) $instance['offset'] )
                
    $offset 0;
    ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title?>" /></p>

            <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
            <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number?>" size="3" /><br />
            <small><?php _e('(at most 15)'); ?></small></p>

            <p><label for="<?php echo $this->get_field_id('offset'); ?>"><?php _e('Offset:'); ?></label>
            <input id="<?php echo $this->get_field_id('offset'); ?>" name="<?php echo $this->get_field_name('offset'); ?>" type="text" value="<?php echo $offset?>" size="3" /><br /></p>
    <?php
        
    }
    }

    function 
    WP_Widget_Recent_Posts_Offset_init() {
        
    unregister_widget('WP_Widget_Recent_Posts');
        
    register_widget('WP_Widget_Recent_Posts_Offset');
    }

    add_action('widgets_init''WP_Widget_Recent_Posts_Offset_init');

  4. #4
    uwiuw is offline Hello World
    Join Date
    Apr 2010
    Location
    bandung - indonesia
    Posts
    2

    Default otto, i got this error in wp 2.9.2

    i got this error :

    <blockquote>Missing argument 2 for WP_Widget::__construct(), called in C:\xampp\htdocs\myblog\bakawan.com\log\wp-includes\widgets.php on line 324 and defined in C:\xampp\htdocs\myblog\bakawan.com\log\wp-includes\widgets.php on line 93</blockquote>

    what is the problem ? ;)
    god give everything i want

  5. #5
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    Well, probably that you shouldn't be replacing the widgets.php with that file. You'd want to put it in a plugin or a theme or something.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •