Home | About | View All Posts

1 Jan 2015

How to show recent posts and number of post views in wordpress template

Wordpress has inbuilt function named as "wp_get_recent_posts()" to show recent or latest posts which is available in "/wp-includes/post.php" path. We can easily list post title, description and other items of the post with the help of this function. We use a plugin - 'WP-PostViews' to show number of post view count.



We can stratified the code requirement to show recent post and its view count into two section -
  1. The wordpress in-built function wp_get_recent_posts()
  2. The Plugin 'post views' functionality output
  3. Comination of above functionality to output the requirement
  1. The wordpress in-built function wp_get_recent_posts()
    Following inputs and details have been written in worpdress 'post.php':

    Syntax:
    <php wp_get_recent_posts( $args = array(), $output = ARRAY_A); ?>

    Argugments(Default as written in "/wp-includes/post.php"):
    $defaults = array(
    'numberposts' => 10, 'offset' => 0, 'category' => 0, 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '',
    'exclude' => '', 'meta_key' => '', 'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish,
     future, pending, private', 'suppress_filters' => true
    );
    

    For multiple post type we place value by using array as like below -
    'post_type'=> array('post','service', 'movie')

    Parameters:
    $args - array, value
    $output - Constant OBJECT, ARRAY_A

    Returns:
    The function produces list of post arrays(default) or objects depending on $output

    Uses:
    $recent_posts = wp_get_recent_posts( $args, ARRAY_A );

  2. The Plugin 'WP-PostViews' functionality output
    This plugins adds a 'views' column at posts listing page in the admin. It also places a custom field - post meta key 'views' respective of every post. We show post count number with help of this post meta key's value by using following wordpress inbuilt function(written in "/wp-includes/post.php") get_post_meta()-

    Syntax:
    <?php get_post_meta($post_id, $key = '', $single = false) ?>

    Uses:
    <php get_post_meta($post_id, 'views', false) ?>
  3. Comination of above functionality to output the requirement
    <?php
    $args = array( 'numberposts' => 10, 'offset' => 0, 'category' => 0, 'orderby' => 'post_date', 'order' => 'DESC',
    'post_type'=> array('post','service'), 'post_status' => 'publish', 'suppress_filters' => true);
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
    ?>
    
    Post Image:
    <?php get_the_post_thumbnail( $recent["ID"], 'thumbnail' );?>
    Post Title:
    <?php echo '<a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a>';?>
    Post Excerpt:
    Post Category:
    <?php
    $category = get_the_category($recent["ID"]); 
    if($category[0]){
    echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
    }
    ?>
    Post Author:
    <?php echo the_author_meta('user_login', $recent["post_author"]);?>
    Post Date:
    <?php echo date( 'F j, Y', strtotime( $recent['post_date'] ) );?>
    Post Views:
    <?php 
    if(get_post_meta($recent["ID"], 'views', true)==false){
    echo "0"; ?>
    total views
    <?php }else{ 
    echo get_post_meta($recent["ID"], 'views', true);?>
    total views
    <?php } ?>
Tags : , ,

0 comments:

Post a Comment