WordPress Theme Development: Recent posts and Most Viewed Posts Loop (Lesson 8)

The WordPress Loop is the PHP code which used to display main content and excerpts of content on WordPress sites. Inside the loop there are some functions that are run by default to display posts. And the great advantage of the wordpress loop is the same PHP tags to display any content inside wordpress sites. Theme developers can format this loop output by using template tags to customize how each post inside the loop is displayed.

Today I am going to php function to display most recent post and most viewed post in our index theme.

To show the latest 6 post in our index with previous style, I add the following php script below:

<?php
    $latest_w3pro_post = new WP_Query(array('posts_per_page' => 6,));
    if ($latest_w3pro_post->have_posts()) :
        echo '<div class="six columns">';
        echo '<h3 class="post-title">Latest Posts</h3> ';
        echo '<ul class="row box">';
        $first_post = 0;
        while ($latest_w3pro_post->have_posts()) {
            $first_post++;
            if ($first_post == 1) {
                $latest_w3pro_post->the_post();
                ?>
                <li><a style="font-weight:bold; color:#A00019" title="<?php printf(esc_attr__('Permalink to %s', 'compass'), the_title_attribute('echo=0')); ?>" href="<?php the_permalink(); ?>" rel="bookmark"> <?php the_title(); ?></a></li>
                <?php
                if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) {
                    echo get_the_post_thumbnail($post->ID);
                } else {
                    main_image();
                }
                ?>
                <?php the_excerpt(); ?>
                <div class="clear"></div>
                <?php
            }
            if ($first_post > 1) {
                $latest_w3pro_post->the_post();
                ?>
                <li><a title="<?php printf(esc_attr__('Permalink to %s', 'compass'), the_title_attribute('echo=0')); ?>" href="<?php the_permalink(); ?>" rel="bookmark"> <?php the_title(); ?></a></li> 
                <?php
            }
            wp_reset_postdata();
        }
        echo '</ul></div>';
    endif;
    ?>

Now to count most viewed posts, I add the following php functions in w3Pro functions.php file:


// Function to display number of posts.
function getPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if ($count == '') {
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0";
    }
    return $count . ' Views';
}

// Function to count views........
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if ($count == '') {
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    } else {
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

// Function to posts views in wordpress admin...
add_filter('manage_posts_columns', 'posts_column_views');
add_action('manage_posts_custom_column', 'posts_custom_column_views', 5, 2);

function posts_column_views($defaults) {
    $defaults['post_views'] = __('Views');
    return $defaults;
}

function posts_custom_column_views($column_name, $id) {
    if ($column_name === 'post_views') {
        echo getPostViews(get_the_ID());
    }
}

Adding this to our single.php, and make sure you paste in inside the loop.

<?php setPostViews(get_the_ID()); ?>

To display the number of views a post has, just adding this in our index template where we want the post view count to be displayed.

<?php echo getPostViews(get_the_ID()); ?>

As usual, now I am going to loop the most viewed post php code below:


<?php
    $most_viewed_w3pro_post = new WP_Query(array(
        'posts_per_page' => 6,
        'orderby' => 'meta_value_num',
        'meta_key' => 'post_views_count'
    ));
    if ($most_viewed_w3pro_post->have_posts()) :
        echo '<div class="six columns">';
        echo '<h3 class="post-title">Most Viewed Posts</h3> ';
        echo '<ul class="row box">';
        $first_post = 0;
        while ($most_viewed_w3pro_post->have_posts()) {
            $first_post++;
            if ($first_post == 1) {
                $most_viewed_w3pro_post->the_post();
                ?>
                <li><a style="font-weight:bold; color:#A00019" title="<?php printf(esc_attr__('Permalink to %s', 'compass'), the_title_attribute('echo=0')); ?>" href="<?php the_permalink(); ?>" rel="bookmark"> <?php the_title(); ?></a>
                    <span style="color:#000"><?php echo " - " . getPostViews(get_the_ID()); ?></span></li>
                <?php
                if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) {
                    echo get_the_post_thumbnail($post->ID);
                } else {
                    main_image();
                }
                ?>
                <?php the_excerpt(); ?>
                <div class="clear"></div>
                <?php
            }
            if ($first_post > 1) {
                $most_viewed_w3pro_post->the_post();
                ?>
                <li><a title="<?php printf(esc_attr__('Permalink to %s', 'compass'), the_title_attribute('echo=0')); ?>" href="<?php the_permalink(); ?>" rel="bookmark"> <?php the_title(); ?></a>
                    <span style="color:#A00019"><?php echo " - " . getPostViews(get_the_ID()); ?></span></li> 
                <?php
            }
            wp_reset_postdata();
        }
        echo '</ul></div>';
    endif;
    ?>

Finally I have add the below php code after the titles of our loop function:

<?php echo getPostViews(get_the_ID()); ?>

And I have got the following preview of our index blog in w3Pro wordpress theme.
recent-and-most-viewed-posts

In the next tutorial I will work on single post php to display the full content.

Hello, This is Rashad. I like programming.
Print Friendly

Leave a Reply

Your email address will not be published. Required fields are marked *

*