Home › Forums › Calendar Products › Community Events › Can I display the number of events on the front end?
- This topic has 6 replies, 3 voices, and was last updated 9 years, 10 months ago by
fifteen15studios.
-
AuthorPosts
-
June 18, 2016 at 10:53 pm #1128982
fifteen15studios
ParticipantI would like to display the number of events a user has created (which are active) on the front end, like on any page using a shortcode or PHP function. Is that possible? How would I go about doing that?
So, for example, if I create there events under a user name, and I’m logged in as that user, I can display the number “3” somewhere on the page?
Thanks,
BrendanJune 19, 2016 at 2:48 pm #1129059Nico
MemberHi there Brendan,
Thanks for reaching out! I can help you here…
Use the snippet below to echo the amount of upcoming events for the current logged in user (if any):
/* Tribe: print active events count for current user if logged in */
function tribe_user_events_count ( ) {if ( $user_id = get_current_user_id() ) {
$events = tribe_get_events( array( 'author' => $user_id, 'eventDisplay' => 'upcoming' ) );
echo count ( $events );
}
}add_action( 'tribe_events_before_template', 'tribe_user_events_count' );
This will show before events views, just as an example. You can change the action or place a call to the function tribe_user_events_count in the desired template.
Please let me know if this works for you,
Best,
NicoJune 20, 2016 at 12:14 am #1129119fifteen15studios
ParticipantNico,
First, THANK YOU for taking the time to explain it in so much detail! I added the code to my functions.php and placed the call in my template. However, it doesn’t seem to be working. It should return a number, but it returns blank. Do you know why that could be? I’m logged in.
Thanks,
BrendanJune 20, 2016 at 3:32 pm #1129553Nico
MemberThanks for following up Brendan! To see if it’s event returning something or not, let’s do the following:
function tribe_user_events_count ( ) {if ( $user_id = get_current_user_id() ) {
$events = tribe_get_events( array( 'author' => $user_id, 'eventDisplay' => 'upcoming' ) );
echo 'User events: ' . count ( $events );
} else {
echo 'No user is recognized: ' . get_current_user_id();
}
}
Also, make sure the user has created at least 1 upcoming event. If you want to get all events for that user not just the upcoming ones, then make the following change:
$events = tribe_get_events( array( 'author' => $user_id ) );
Please let me know about the result of using the modified version of the function above,
Best,
NicoJune 21, 2016 at 8:07 am #1129796fifteen15studios
ParticipantOk, thanks! The code worked before, but that’s an enhancement. Apparently when I call a function, sometimes the output gets moved around the page. I’m not sure why. It must be some other script interfering. But I got it to work! The output wasn’t in the place I expected.
June 21, 2016 at 8:48 am #1129845Nico
MemberGlad to hear Brendan 🙂
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.
Best,
Nico -
AuthorPosts
- The topic ‘Can I display the number of events on the front end?’ is closed to new replies.
