47 posts
    Has been part of the Envato Community for over 4 years Has collected 1+ items on Envato Market
virgild says

I’m trying to develop a widget for the popular posts tutorial here http://bavotasan.com/tutorials/how-to-list-your-most-popular-posts-in-wordpress/

I’m not sure how to replace the $num variable in functions.php with

$options['number'];?> which let’s you pick the number of posts to list. I would think that the best way is to incorporate the entire function into the widget but I’m not sure how.

Can someone please help?

629 posts
    Has been part of the Envato Community for over 4 years Has referred 10+ members Has collected 10+ items on Envato Market Sells items exclusively on Envato Market
+2 more
VagrantRadio says

The first block of code goes into your functions.php file and the second one goes in a text widget. This is how you set the number of posts to show where “10” is the number.

<?php echo popularPosts(10); ?>
47 posts
    Has been part of the Envato Community for over 4 years Has collected 1+ items on Envato Market
virgild says

Thanks Vagrant I know that part but how to I set the number of posts via a widget option? $options['number'];?>

629 posts
    Has been part of the Envato Community for over 4 years Has referred 10+ members Has collected 10+ items on Envato Market Sells items exclusively on Envato Market
+2 more
VagrantRadio says
Thanks Vagrant I know that part but how to I set the number of posts via a widget option? $options['number'];?>

Where are you getting this options number code from?

You just put the above php code in the widget and “(10)” is how you set the number of posts.

629 posts
    Has been part of the Envato Community for over 4 years Has referred 10+ members Has collected 10+ items on Envato Market Sells items exclusively on Envato Market
+2 more
VagrantRadio says

in your functions file:

function popularPosts($num) {
    global $wpdb;

    $posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");

    foreach ($posts as $post) {
        setup_postdata($post);
        $id = $post->ID;
        $title = $post->post_title;
        $count = $post->comment_count;

        if ($count != 0) {
            $popular .= '<li>';
            $popular .= '<a href="' . get_permalink($id) . '" title="' . $title . '">' . $title . '</a> ';
            $popular .= '</li>';
        }
    }
    return $popular;
}

in your text widget:

<?php echo popularPosts(10); ?>  

popularPosts(10) is your number of posts.

707 posts
    Has referred 10+ members Has sold $10,000+ on Envato Market Has been a beta tester for an Envato feature Has collected 10+ items on Envato Market
+2 more
ChillThemes says
by
by
by
by
by
by