Forums

[resolved] Post Count and Word count per Tag (9 posts)

  1. deanacus
    Member
    Posted 2 years ago #

    I am doing a redesign on my blog at the moment, and one thing that I am trying to do is include on my Tag archive pages some text along the lines of

    "This is the TAG archive. There are XX posts tagged with TAG, with a total of XXX words."

    I've got it working on my normal archive page, which displays every post on my blog. To do this I've use the Post Count and the Post Word Count plugins, slightly modified to exclude pages. I'd love to know some way I can modify these plugins to extract the data I want, as I'd like to keep the logic as separate from the templates as possible

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    Could put this in a tag template:

    <?php
    if ( is_tag() ) {
      $tag = get_query_var('tag');
      $term = get_term_by('name',$tag, 'post_tag');
      $wordcount=0;
      $args=array(
        'tag__in' => array($term->term_id),
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
        );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <?php
          $postc = strip_tags($post->post_content); //strip html and php tags http://us2.php.net/manual/en/function.strip-tags.php
          $poste = explode(' ', $postc); //explode into an array, assume each word is separated by a space
          $count = count($poste); // count elements of array
          $wordcount = $count + $wordcount; //keep a running count
        endwhile;
        echo '<p>Number of posts in tag '.$tag . ': ' . $my_query->post_count .'</p>';
        echo '<p>Number of words in tag '.$tag . ' posts: '. $wordcount .'</p>';
      }
    wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>
  3. deanacus
    Member
    Posted 2 years ago #

    Wow, thats awesome.

    Thankyou.

    One more question though. Is there a way that I could take that out of the template and put it into a plugin, or just keep the variables so that I can call them elsewhere in the template?

    If you don't have the inclination, or the ability, thats cool. thanks for your help.

  4. MichaelH
    Volunteer
    Posted 2 years ago #

    Don't know why you couldn't put that in a plugin.

  5. deanacus
    Member
    Posted 2 years ago #

    Sweet. I'll give it a go. Thanks so much for your help

  6. deanacus
    Member
    Posted 2 years ago #

    Actually, when I put it into a plugin, the post count works perfectly, but the word count displays the post count also. Plugin code is below. The IF statement is just there for if the post count == 1.

    <?php
    /*
    Plugin Name: Tag Word And Post Count
    Plugin URI: http://wordpress.org/support/topic/355846
    Description: Outputs the total number of words and the number of posts within a tag archive page.
    Version: 0.1
    Author: Deanacus/WP.org forum mod MichaelH
    Author URI: http://wordpress.org/support/topic/355846
    */
    
    function tag_count_para() {
      $tag = get_query_var('tag');
      $term = get_term_by('name',$tag, 'post_tag');
      $wordcount=0;
      $args=array(
        'tag__in' => array($term->term_id),
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
        );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <?php
          $postc = strip_tags($post->post_content); //strip html and php tags http://us2.php.net/manual/en/function.strip-tags.php
          $poste = explode(' ', $postc); //explode into an array, assume each word is separated by a space
          $count = count($poste); // count elements of array
          $wordcount = $count + $wordcount; //keep a running count
        endwhile;
        if ($my_query->post_count>1){
        echo '<p>This is the archive of posts tagged with &ldquo;'.$tag . '&rdquo;. There are ' . $my_query->post_count .' posts tagged with &ldquo;'.$tag . '&rdquo;, with a total of '. $wordcount .' words.</p>';
        }
        if ($my_query->post_count==1){
        echo '<p>This is the archive of posts tagged with &ldquo;'.$tag . '&rdquo;. There is ' . $my_query->post_count .' post tagged with &ldquo;'.$tag . '&rdquo;, with a total of '. $wordcount .' words.</p>';
        }
      }
     }
    ?>
  7. MichaelH
    Volunteer
    Posted 2 years ago #

    After this

    function tag_count_para() {

    add this:

    global $post;
  8. deanacus
    Member
    Posted 2 years ago #

    Thank you. Thank you very very much.

  9. MichaelH
    Volunteer
    Posted 2 years ago #

    You're welcome. Glad it worked.

Topic Closed

This topic has been closed to new replies.

About this Topic