WordPress.org

Ready to get started?Download WordPress

Forums

Place Total comment count in page title (4 posts)

  1. nblauw
    Member
    Posted 11 months ago #

    Hello,

    How can I put the Total comment count of al posts and pages in a page title?
    Like " all comments (46)"

    My blog: bartbroersen.nl
    The page I talk about: bartbroersen.nl/reacties

    Thanks in advance!
    Nick

  2. singsolo
    Member
    Posted 11 months ago #

    Well, you can pull the total number of comments from the database. The function would look something like:

    function add_comment_count($content = null) {
         global $wpdb;
    // Run the Query to calculate the total comment count and store the result in the $comment_count variable
         $comment_count = $wpdb->get_row("SELECT count(comment_approved) AS cmmnt_cnt FROM comments WHERE comment_approved = '1' GROUP BY comment_approved", ARRAY_A);
    // Add the count to the end of the content, in this case, the title
         $content .= '('.$comment_count.' Total Comments)';
    //  Finally, return the modified content to be displayed
         return $content;
    }

    Then you would need to add a filter, probably on the "single_post_title" hook.

    add_filter('single_post_title', 'add_comment_count', 5);

    That should get the job done for you.

  3. singsolo
    Member
    Posted 11 months ago #

    I think I may have misread where you want the count. I was, for some reason, thinking you wanted it in the post title, but you want it in the page title.

    Do you want it in all page titles, or just one specific page? If you want it in all page titles, then the function above should work fine, but your filter would be different.

    add_filter('wp_title', 'add_comment_count');

    If you are trying to do it on a specific page, you would need to check for that in your function.

  4. nblauw
    Member
    Posted 11 months ago #

    I want to do it on a specific page, with a shortcode.
    Like: alle reacties ([comment_count])
    I did it the Same with DMSguestbook comments and the total of images in a gallery as you can See on my blog.

    Thanks for answering!
    Nick

Reply

You must log in to post.

About this Topic