Can I display the number of events on the front end?

Home Forums Calendar Products Community Events Can I display the number of events on the front end?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1128982
    fifteen15studios
    Participant

    I 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,
    Brendan

    #1129059
    Nico
    Member

    Hi 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,
    Nico

    #1129119
    fifteen15studios
    Participant

    Nico,

    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,
    Brendan

    #1129553
    Nico
    Member

    Thanks 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,
    Nico

    #1129796
    fifteen15studios
    Participant

    Ok, 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.

    #1129845
    Nico
    Member

    Glad 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

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Can I display the number of events on the front end?’ is closed to new replies.