Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to display post view in localize number. I add these function in function.php to do so

function make_bangla_number($str)
{
    $engNumber = array(1,2,3,4,5,6,7,8,9,0);
    $bangNumber = array('১','২','৩','৪','৫','৬','৭','৮','৯','০');
    $converted = str_replace($engNumber, $bangNumber, $str);

    return $converted;
}

add_filter( 'the_views', 'make_bangla_number' );

But I am unable to show the number in localize. Whenever i call the_views it shows the english number. Any idea how to show the post view number in localize language?

For further info here is my post view function:

// function to count post views.
function setPostViews($postID) {
    $count_key = 'views';
    $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 display number of post views.
function the_views($postID){
    $count_key = 'views';
    $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 View";
    }
    return $count.' বার';
}

I have Bangla language pack installed and my site CHARSET is also UTF-8. Bangla language pack does all but can't convert the English number into bangla number. So I using that code. With that i can convert the date number in Bangla but unable to convert the views. So I am here.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

The problem is most probably, that you don't actually call the filter. WordPress does this not automatically for you.

If you want to create a custom filter you have to manually call apply_filters, so that the added filters get executed:

function the_views($postID){
    $count_key = 'views';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        $count = 0;
    }

    // use apply_filters to call the filter
    return sprintf($count == 0 ? '%s View' : '%s বার', 
        apply_filters('the_views', $count));

    // better alternative: use the WordPress translation API:
    //return sprintf(_n('%s View', '%s Views', $count), 
        //apply_filters('the_views', $count));
}

I have modified the code a bit so the digit "0" gets also translated/converted. I have also added a suggestion at the end to use the WP translation API. There's a bit more effort required to translate it that way but it would bring some benefits (you could translate it to other languages without code-changes and the result would be proper pluralized).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.