• Resolved katrianna

    (@katrianna)


    Hi Ajay,

    Thanks for your response and apologies for not posting my query here in the first place.

    So I’d like to add a comment count to your plugin. So far I’ve amended the top-10.php file to include the following lines which show up in the Admin fine, I can select the option, but it’s not outputting on the front end correctly:

    line 448:
    if ($show_comments) { $output .= '<span class="tptn_comments"> '.comments_number( '0', '1', '%' ).'</span>';

    line 538:
    $show_comments = isset($instance['show_comments']) ? esc_attr($instance['show_comments']) : '';

    line 586:

    <p>
    <label for="<?php echo $this->get_field_id('show_comments'); ?>">
    <input id="<?php echo $this->get_field_id('show_comments'); ?>" name="<?php echo $this->get_field_name('show_comments'); ?>" type="checkbox" <?php if ($show_comments) echo 'checked="checked"' ?> /> <?php _e(' Show comments?', TPTN_LOCAL_NAME); ?>
    </label>
    </p>

    line 622:
    $instance['show_comments'] = ($new_instance['show_comments']);

    line 658:
    'show_comments' => $instance['show_comments'],

    line 675:
    'show_comments' => $instance['show_comments'],

    line 789:
    'show_comments' => false, // Show comments in list item'

    I’d also be happy to just hardcode the comment counter in there (without the if statement), instead of making it a selectable option, so I tried the following on line 448 but again, I’m clearly doing something wrong as it’s not outputting correctly:

    $output .= '<span class="tptn_comments">'.comments_number( '0', '1', '%' ).'</span> ';

    Your help would be great.
    Many thanks
    Katri

    https://wordpress.org/plugins/top-10/

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Ajay

    (@ajay)

    I think this is because comments_number should work only within the loop for the current posts.

    You should use wp_count_comments to get the number of comments.

    http://codex.wordpress.org/Function_Reference/wp_count_comments

    Can you try something like?

    if ($show_comments) {
    $comments_count = wp_count_comments( $result->ID );
    $output .= '<span class="tptn_comments"> '.$comments_count->approved.'</span>'
    }
    Thread Starter katrianna

    (@katrianna)

    Worked like a charm, thanks so much Ajay!

    Thread Starter katrianna

    (@katrianna)

    Hey Ajay, sorry to bother you again – do you know if it’s possible to link through to the comments using this?

    Thread Starter katrianna

    (@katrianna)

    Hi Ajay, I have another question for you – I’m also trying to integrate the post counts into a different widget (special recent posts). I’m currently using this:

    $srp_content .= "<span class='srp-post-count'>";						if(function_exists('echo_tptn_post_count')) echo_tptn_post_count();
    $srp_content .= "</span>";

    but the counter is not outputting within the span, instead it’s outputting this at the very top of the widget:

    <div class ="tptn_counter" id="tptn_counter_117">...</div>

    Do you know how I can get it to sit within the span?

    Thanks so much in advance

    Thread Starter katrianna

    (@katrianna)

    Those first lines didn’t print correctly, this is what I’m placing within the span above:

    if(function_exists('echo_tptn_post_count')) echo_tptn_post_count();

    Thread Starter katrianna

    (@katrianna)

    Not to worry, I figured it out! If you do get a chance to help me out with the comments link though that’d be great. Thanks Ajay!

    Thread Starter katrianna

    (@katrianna)

    I spoke too soon – I’m using this now but it’s still printing with the other div at the top of the widget 🙁

    $srp_content .= '<span class="srp-post-count">'.echo_tptn_post_count().'</span>';

    Plugin Author Ajay

    (@ajay)

    You need to use this function to get only the post count.

    get_tptn_post_count_only($id = FALSE, $count = 'total')

    Something like:

    <?php echo get_tptn_post_count_only($result-ID, 'total') ?>

    The important part with will passing the ID. You can use ‘total’ or ‘daily’ to fetch the corresponding count.

    What do you mean by “link through to the comments”?
    If I’m not mistaken it usually is a permalink of the post followed by #comments

    Thread Starter katrianna

    (@katrianna)

    Hi Ajay, thanks so much for that.

    I’ve got this in the Special recent Posts widget now:

    $srp_content .= '<span class="srp-post-count">'.get_tptn_post_count_only($id = FALSE, $count = 'total').'</span>';

    It’s displaying in the correct place now which is great but it’s not printing the account, it’s staying at 0.

    With regard to the comments display in Top 10, apologies for not explaining myself properly – yep, I meant the permalink. At the moment the permalink isn’t getting outputted with the following, it just displays the number:

    if ($show_comments) {
    $comments_count = wp_count_comments( $result->ID );
    $output .= '<span class="tptn_comments"> '.$comments_count->approved.'</span>';
    }

    Thanks again

    Plugin Author Ajay

    (@ajay)

    You need to pass the ID of the post to the function otherwise it’s going to just give you a 0.

    i.e. $id parameter needs to be the post ID.

    Something like:

    <?php echo get_tptn_post_count_only($result-ID, 'total') ?>

    Thread Starter katrianna

    (@katrianna)

    Ah thanks Ajay, I got it working with $post->ID

    With the comments display in your plugin, I got it working with the permalink with this:

    if ($show_comments) {
    $comments_count = wp_count_comments( $result->ID );
    $output .= '<a class="tptn_comments" href="'.get_permalink($result->ID). '" class="comments_link">' . $comments_count->approved . '</a>';
    }

    Thanks again for all your help

    Plugin Author Ajay

    (@ajay)

    You’re welcome.

    You might also want to try:

    if ($show_comments) {
    $comments_count = wp_count_comments( $result->ID );
    $output .= '<a class="tptn_comments" href="'.get_permalink($result->ID). '#comments" class="comments_link">' . $comments_count->approved . '</a>';
    }

    It might jump to the comments directly depending on your theme.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘add comment count’ is closed to new replies.