WordPress.org

Ready to get started?Download WordPress

Forums

Event Organiser
[resolved] Sidebar Loop to display Upcoming Events (5 posts)

  1. daba
    Member
    Posted 1 year ago #

    I'm trying to create a custom sidebar that displays the next 3 upcoming events. This sidebar is to be displayed alongside my blog page and so the code keeps grabbing posts instead of events.

    How could I change my current loop (below) to output events instead of posts? Is this possible?

    <?php query_posts('showposts=3'); ?>
      <aside class="module">
        <h3>Latest News</h3>
        <ul>
    
          <?php while (have_posts()) : the_post(); ?>
            <li><a href='<?php the_permalink() ?>'>
              <h5><?php the_title(); ?></h5>
              <p><?php echo excerpt(15); ?></p>
            </a></li>
          <?php endwhile; ?>
    
        </ul>
      </aside>

    Any suggestions would be highly appreciated. Thanks. :)

    [I'm using a separate function to shorten the excerpt, please ignore that]

    http://wordpress.org/extend/plugins/event-organiser/

  2. Stephen Harris
    Member
    Plugin Author

    Posted 1 year ago #

    Hi daba,

    First you shouldn't use query_posts().

    In this specific instance, I'd normally suggest get_posts() - but the plug-in provides its own version of that specifically for events: eo_get_events() (see documentation):

    //Get upcoming '
    $events = eo_get_events(array(
         'numberposts'=>5,
         'events_start_after'=>'today',
         'showpastevents'=>true,//Deprecated, but set it to true to play it safe.
      ));
    
    if( $events ){
        global $post;
        foreach( $events as $post ){
             setup_postdata($post);
             ?>
          <li>
               <a href='<?php the_permalink() ?>'>
                 <h5><?php the_title(); ?></h5>
                 <p><?php the_excerpt(); ?></p>
               </a>
               on <?php eo_get_the_start('jS F Y'); ?>
          </li>
             <?php
         }
         wp_reset_postdata();
    }

    I've not tested this, but syntax errors excepting, it should work!

  3. daba
    Member
    Posted 1 year ago #

    Yes it works thank you so much.

    Also is is possible to add an else statement to the above loop of yours in order to display something like "No Upcoming Events" when no events are available?

    Sorry, but my php is really basic at the moment...

  4. Stephen Harris
    Member
    Plugin Author

    Posted 1 year ago #

    Yup just add and else statement. I've edited the above example, included an else statement, and made it into this gist.

  5. daba
    Member
    Posted 1 year ago #

    Thank you so much and have a great day :D

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags