WordPress.org

Ready to get started?Download WordPress

Forums

WP-PostViews
Added a function to get post views by post ID (2 posts)

  1. fatchoumakou
    Member
    Posted 1 year ago #

    Hello

    I added a function in the plugin to get post views by post ID (get_the_views).
    I also modified 'the_views' function to handle this new function.

    Hope this helps !

    ### Function: Display The Post Views
    function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
        $views = get_the_views(null, $prefix, $postfix, $always);
        if ($display) {
           echo $views;
        }  else {
           return $views;
        }
    }
    
    ### Function: Display The Post Views By Post ID
    if (!function_exists('get_the_views')) {
        function get_the_views($post_id = null, $prefix = '', $postfix = '', $always = false)
        {
            $post_id = (null === $post_id) ? get_the_ID() : $post_id;
            $custom_fields = get_post_custom_values('views', $post_id);
            $post_views = intval($custom_fields[0]);
            $views_options = get_option('views_options');
    
            if ($always || should_views_be_displayed($views_options)) {
                $output = $prefix . str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $views_options['template']) . $postfix;
                return apply_filters('the_views', $output);
            }
            return '';
        }
    }

    http://wordpress.org/extend/plugins/wp-postviews/

  2. astrasuite
    Member
    Posted 1 year ago #

    Thanks !

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.