Counting posts

Home Forums Calendar Products Events Calendar PRO Counting posts

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1142880
    Hans-Gerd
    Participant

    Hi,
    I want to count posts of each category and show the results in a widget or before the loop in loop.php of the list view (child theme).
    I have try someting like this, but unfortunately there appear no results:
    <?php wp_list_cats('sort_column=name&sort_order=asc&optioncount=1&use_desc_for_title=0&child_of=3'); ?>
    (https://codex.wordpress.org/Stepping_Into_Template_Tags)

    In the following example all posts are counted but not distinguished after categories:
    <span class=”gross”><?php $nr_art = wp_count_posts(‘tribe_events’); $nr_art = $nr_art->publish; echo number_format($nr_art, 0, ”, ‘.’); ?></span> Termine und Veranstaltungen<br>

    Best regards
    Hans-Gerd

    #1143134
    Nico
    Member

    Hey Hans,

    Thanks for getting in touch! I crafted a little helper function to do this:


    function tribe_count_by_cat ( $event_category_slug ) {

    if ( ! class_exists('Tribe__Events__Main') ) return false;

    $tax_query = array( 'taxonomy' => Tribe__Events__Main::TAXONOMY,
    'field' => 'slug',
    'terms' => $event_category_slug );

    $args = array( 'post_type' => Tribe__Events__Main::POSTTYPE, 'post_status' => 'publish', 'tax_query' => array( $tax_query ), 'posts_per_page' => -1);

    $query = new WP_Query( $args );

    return $query->found_posts;
    }

    Add this code to your theme (or child theme) functions.php file and just call the function in your template like this:

    // get events count for Sports event category
    echo tribe_count_by_cat('sports');

    Please let me know if this is what you were looking for,
    Best,
    Nico

    #1143160
    Hans-Gerd
    Participant

    Hi Nico,
    great – it works. Many thanks for assistance.

    There is only a little problem: I would like to receive the category automatically
    instead of:
    echo tribe_count_by_cat('sports');
    this one:
    echo tribe_count_by_cat('what_ever_I_need');

    For example:
    ../events/category/terminanzeige/
    or
    ../events/category/Ferien/

    Best regards
    Hans-Gerd

    #1143215
    Nico
    Member

    Thanks for following up Hans-Gerd!

    So you want to pull the current category from the URL? And if there’s no category just return an empty string?

    Please let me know so I can tweak the code 🙂

    #1143283
    Hans-Gerd
    Participant

    Hi Nico,
    thanks for answer.

    So you want to pull the current category from the URL? And if there’s no category just return an empty string?

    yes, this is exactly that what I need.

    Best regards
    Hans-Gerd

    • This reply was modified 9 years, 9 months ago by Hans-Gerd.
    #1143285
    Hans-Gerd
    Participant

    Hi Nico,
    I have found the solution – changed in loop.php:

     <?php
      // get events count for every event category
      $kategorie = tribe_meta_event_category_name();
      echo tribe_count_by_cat($kategorie) . ' Veranstaltungen';
      ?>
    

    Many thanks for assistance and best regards
    Hans-Gerd

    #1143577
    Hans-Gerd
    Participant

    Hi Nico,
    another better solution:

      <?php
      // get events count for every event category
      $kategorie = tribe_meta_event_category_name();
      // change name of category, if name and slug is not identical:
      if ($kategorie == 'XXLPremium') {
        $kategorie = 'premium';
      }
      // category name is empty
      if ($kategorie != '') {  
        echo $kategorie . ': ';
        echo tribe_count_by_cat($kategorie) . ' Veranstaltungen';
      }
      ?>
    

    Best regards
    Hans-Gerd

    #1143674
    Nico
    Member

    Glad to see you could get this done Hans-Gerd 🙂

    Thanks for sharing the complete code you used!

    I’ll go ahead and close out this thread, but if you need help with anything else please don’t hesitate to create a new one and we will be happy to assist you.

    Hope you have a great week,
    Nico

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Counting posts’ is closed to new replies.