WordPress.org

Ready to get started?Download WordPress

Forums

WordPress Popular Posts
[resolved] Remove the (s) in view(s) and comment(s) (17 posts)

  1. rextherunt
    Member
    Posted 2 years ago #

    Hi
    I wondered if it's possible to remove the (s) from the stats display?
    At the moment it looks like this:

    Poster 1 3 comment(s) | 62 view(s)
    Poster 6 0 comment(s) | 1 view(s)

    Surely it should rather display 1 comment or 3 comments, not 1 comment(s) and 3 comment(s)

    I hope I'm clear enough.
    Thank you!

    http://wordpress.org/extend/plugins/wordpress-popular-posts/

  2. Marventus
    Member
    Posted 2 years ago #

    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!

  3. rextherunt
    Member
    Posted 2 years ago #

    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!

  4. Marventus
    Member
    Posted 2 years ago #

    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.

  5. rextherunt
    Member
    Posted 2 years ago #

    Thank you!

  6. Marventus
    Member
    Posted 2 years ago #

    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>";
    }
  7. rextherunt
    Member
    Posted 2 years ago #

    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!

  8. Marventus
    Member
    Posted 2 years ago #

    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>";
    }
  9. rextherunt
    Member
    Posted 2 years ago #

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

  10. Marventus
    Member
    Posted 2 years ago #

    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.

  11. Marventus
    Member
    Posted 2 years ago #

    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.
    }

  12. rextherunt
    Member
    Posted 2 years ago #

    Nope, breaks the page again.
    :(

  13. rextherunt
    Member
    Posted 2 years ago #

    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!

  14. Marventus
    Member
    Posted 2 years ago #

    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>";
    }
  15. Marventus
    Member
    Posted 2 years ago #

    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.

  16. rextherunt
    Member
    Posted 2 years ago #

    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!

  17. Marventus
    Member
    Posted 2 years ago #

    Awesome! Good thing it worked because I was running out of ideas, ;-)
    Happy blogging!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic