Home › Forums › Calendar Products › Events Calendar PRO › Counting posts
- This topic has 8 replies, 3 voices, and was last updated 9 years, 9 months ago by
Hans-Gerd.
-
AuthorPosts
-
July 22, 2016 at 4:42 am #1142880
Hans-Gerd
ParticipantHi,
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-GerdJuly 22, 2016 at 12:30 pm #1143134Nico
MemberHey 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,
NicoJuly 22, 2016 at 1:14 pm #1143160Hans-Gerd
ParticipantHi 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-GerdJuly 22, 2016 at 2:33 pm #1143215Nico
MemberThanks 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 🙂
July 22, 2016 at 10:36 pm #1143283Hans-Gerd
ParticipantHi 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.
July 22, 2016 at 10:56 pm #1143285Hans-Gerd
ParticipantHi 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-GerdJuly 25, 2016 at 1:13 am #1143577Hans-Gerd
ParticipantHi 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-GerdJuly 25, 2016 at 9:04 am #1143674Nico
MemberGlad 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 -
This reply was modified 9 years, 9 months ago by
-
AuthorPosts
- The topic ‘Counting posts’ is closed to new replies.
