Viewing 15 replies - 1 through 15 (of 16 total)
  • Hi Rex,

    In order to do that, you would have to edit the plugin file called wordpress-popular-posts.php. Before you do, make a backup of that file.
    Open that file on your computer with any html or text editor. Do a search for the following text:

    // build stats tag

    You will have to make two changes in that section of the code.

    1. First Change:
    Replace this code in line 672:

    $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comment(s)', 'wordpress-popular-posts') . "</span>";

    with this one:

    if ( $comment_count == '1' ) {
    $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comment', 'wordpress-popular-posts') . "</span>";
    } else {
    $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comments', 'wordpress-popular-posts') . "</span>";
    }

    2. Second Change:
    Replace this code in line 675:

    $views_text = __(' view(s)', 'wordpress-popular-posts');

    with this one:

    if ( $views_text == '1' ) {
    $views_text = __(' view', 'wordpress-popular-posts');
    } else {
    $views_text = __(' views', 'wordpress-popular-posts');
    }

    That should do the trick.

    Cheers!

    Thread Starter rextherunt

    (@rextherunt)

    Hi Marventus
    Thank you for being so helpful, I appreciate it. It’s almost right, but a little off…

    Poster 1 3 comments | 62 view
    Poster 2 1 comment | 20 view
    Poster 3 0 comment | 5 view

    View needs to be views if more than 1 and 0 comment should also be 0 comments. In other words, the only case where it’s singular is for 1.

    Do you agree?
    Thanks again!

    Hi,

    That’s what the code I posted should be doing (in theory). Let me test it on my test blog see if there’s something I’m not seeing.

    Thread Starter rextherunt

    (@rextherunt)

    Thank you!

    I can’t really see the code I posted in action because there is no sufficient data on my test site to show popular posts.
    As for the info you shared, the ‘views’ code is not working because I was testing the wrong function. That being said, before we fix that, let’s first try to make it work on comments.
    Try removing the single quotes around ‘1’, and post your results. The modified code should be:

    if ( $comment_count == 1 ) {
    $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comment', 'wordpress-popular-posts') . "</span>";
    } else {
    $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comments', 'wordpress-popular-posts') . "</span>";
    }

    Thread Starter rextherunt

    (@rextherunt)

    Ok, removed the quotes and still the same:

    Poster 1 3 comments | 62 view
    Poster 2 1 comment | 20 view
    Poster 3 0 comment | 5 view

    But I guess “no comment” is also how you can read it!

    Ok, I don’t really understand why the function is returning “comment” for a zero value. Let’s try one more thing:
    Change:

    if ( $comment_count == 1 ) {

    to:

    if ( $comment_count === 1 ) {

    If that doesn’t work, replace the entire code I gave you for the comments with this one:

    if ( $comment_count == 0 || $comment_count > 1 ) {
    $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comments', 'wordpress-popular-posts') . "</span>";
    } else {
    $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comment', 'wordpress-popular-posts') . "</span>";
    }

    Thread Starter rextherunt

    (@rextherunt)

    Sorry, none of the above worked – the new code broke the page.
    And more than one = will break it too.

    Hey Rex,

    This is really puzzling. The code I provided the first time around should be evaluating the comment_count variable accurately.
    To give you an example, this code:

    $origin = 0;
    $comment_count = (int) $origin;
    if ( $comment_count == 1 ) {
    echo 'comment';
    } else {
    echo 'comments';
    }

    works in a very similar way as the code inside the plugin. It prints “comment” if the value of ‘origin’ is 1, but “comments” if it is any other integer than 1 (0, 2, 3, etc.). In this case, the example would return “comments”.
    The problem probably lies in the way in which the comment count value is passed before it reaches the variable. I’ll run some additional tests see if I can work around it.

    Ok, I tested this with a DB query of my own, and it turns I was right: the issue arises from passing the comment_count value to $comment_count and then to a different variable.
    I was able to fix this on my query by checking the comment count value directly from the plugin’s DB query. Try this code instead of the one I provided above:

    if ( $wppost->comment_count == 1 ) {
    $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comment', 'wordpress-popular-posts') . "</span>";
    } else {
    $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comments', 'wordpress-popular-posts') . "</span>";

    Let me know if this works.
    }

    Thread Starter rextherunt

    (@rextherunt)

    Nope, breaks the page again.
    ??

    Thread Starter rextherunt

    (@rextherunt)

    I’m happy with the comments as they are now – I can live with 0 comment being read as “no comment”.
    Is it easy to fix the views the same way?

    Poster 5 0 comment | 2 view
    Poster 6 3 comments | 1 view

    Thanks for your time!

    Sorry about that: I left a closing bracket outside of the code box. This should work:

    if ( $wppost->comment_count == 1 ) {
    $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comment', 'wordpress-popular-posts') . "</span>";
    } else {
    $post_stats .= "<span class=\"wpp-comments\">" . $comment_count . " " . __(' comments', 'wordpress-popular-posts') . "</span>";
    }

    As for the views, you have to make two changes to the code:
    First change:
    Replace line 675:

    $views_text = __(' view(s)', 'wordpress-popular-posts');

    with this code:

    if ( $wppost->pageviews == 1 ) {
    $views_text = __(' view', 'wordpress-popular-posts');
    } else {
    $views_text = __(' views', 'wordpress-popular-posts');
    }

    Second change:
    Replace line 680:

    if ($instance['range'] != 'daily') $views_text = __(' view(s) per day', 'wordpress-popular-posts');

    with this code:

    if ($instance['range'] != 'daily') $views_text .= __(' per day', 'wordpress-popular-posts');

    That should do it.
    Keep in mind that these changes to the plugin file will most likely get overwritten with the next update, so backup your moded file before updating.

    Thread Starter rextherunt

    (@rextherunt)

    Hi again

    It’s working!

    Poster 1 3 comments | 71 views
    Poster 2 1 comment | 20 views
    Poster 3 0 comments | 1 view

    Thanks for persevering, I appreciate it!

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘[Plugin: WordPress Popular Posts] Remove the (s) in view(s) and comment(s)’ is closed to new replies.