Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+6 votes
960 views
in Q2A Core by
edited by

If you are trying to short view count (or any other number) in q2a then simply add this to theme file:

        function short_num($num, $precision = 2) {
           if ($num >= 1000 && $num < 1000000) {
            $n_format = number_format($num/1000,$precision).'K';
            } else if ($num >= 1000000 && $num < 1000000000) {
            $n_format = number_format($num/1000000,$precision).'M';
           } else if ($num >= 1000000000) {
           $n_format=number_format($num/1000000000,$precision).'B';
           } else {
           $n_format = $num;
            }
          return $n_format;
        }

Like you want to short view count in question list, add this code to theme file,

        function q_item_stats($q_item)
        {
            $this->output('<DIV CLASS="qa-q-item-stats">');
            
            $this->voting($q_item);
            $this->a_count($q_item);
            $this->output(
                '<SPAN CLASS="q-list-points">',
                $this->short_num($q_item['raw']['points']),
                '</SPAN>'
            );
           
        }

This code was tested in 1.6

Q2A version: 1.6 dev
by
edited
thanx but its not working ....in my 1.6 dev
     function q_item_stats($q_item)
        {
            $this->output('<DIV CLASS="qa-q-item-stats">');
             
            $this->voting($q_item);
            $this->a_count($q_item);
            $this->output(
                '<SPAN CLASS="q-list-points">',
                $this->short_num($q_item['raw']['views']),
                '</SPAN>'
            );
            
        }
it breaks my whole theme

1 Answer

+4 votes
by

This is possible from an advanced theme file. You should override the function view_count. Best way would be like this:

function view_count($post)
{
    $post['views']['data'] = preg_replace('/000$/', 'k', $post['views']['data']);
    parent::view_count($post);
}

Note: I haven't tested whether that preg_replace will work, you might need something else.

by
Scott, thanks for the help
by
@Scott  : It's not working in ver 1.6 dev
by
@Scott  : It's not working in ver 1.6 dev please test it
code by @Aryan Rahul breaks my whole theme
i need the code which override the function view_count. only like your code
...