0

I have already found a way to display recent posts:

<?php
                $args = array(
                    'post_status' => 'publish', 
                    'numberposts' => '60', 
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'post_format',
                            'field' => 'slug',
                            'terms' => 'post-format-aside',
                            'operator' => 'NOT IN'
                            ), 
                        array(
                            'taxonomy' => 'post_format',
                            'field' => 'slug',
                            'terms' => 'post-format-image',
                            'operator' => 'NOT IN'
                            )
                        ) 
                    );
                $recent_posts = wp_get_recent_posts( $args );
                ?>

                <?php 
                // This will make a URL like http://yoursite.com/path/to/fallback.png
                $fallback_image = site_url( 'wp-content/themes/gratia/img/fallback-featured-image.jpg' ); 
                $fallback_image = "<img src='{$fallback_image}' />";

                foreach( $recent_posts as $recent ){        
                    echo '<div class="sidebar-entries">';
                    $featured_image = get_the_post_thumbnail( $recent['ID'], 'sidebar-thumb', array( 'class' => 'sidebar-image' ) );

                    if ( ! strlen( $featured_image ) ) {
                        $featured_image = $fallback_image;
                    }

                    $permalink = '<a href="' . get_permalink( $recent['ID'] ) . '">%s</a>';

                    echo sprintf( $permalink, $featured_image );

                    echo '<div class="sidebar-entries-title">';
                    echo sprintf( $permalink, __( $recent["post_title"] ) );
                    echo '<div class="sidebar-date">';

                    $timestamp = '<p>' . human_time_diff( strtotime( $recent['post_date'] ), current_time('timestamp') ) . ' ago </p>';
                    echo sprintf( $permalink, $timestamp );
                    echo '</div>';
                    echo '</div>';
                    echo '</div>';
                }
                ?>

Now I found a function that enables display of the most recent posts:

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;}

My question is, how can I make a list of the most popular posts with the same css classes and preferably a fallback featured image as in the code provided (top)?

  • n it is not possible, first you need to get a source for the popularity ranking – Mark Kaplun Apr 25 '16 at 19:29
  • Updated with proper function. – Arete Apr 25 '16 at 19:38
  • so by popular you mean most commented? – Mark Kaplun Apr 25 '16 at 19:39
  • I assume there is no other way to get the most popular posts without using a plugin, so in that case, yes. – Arete Apr 25 '16 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.