Calling a list of events
-
I have an option on my home page to show events that have been selected to a certain category (Home Featured Event) but as the client doesn’t always select one, I need to have just the next event displayed when none have been selected. I think it is an else command but can’t see where to place it. Below is my code for the current block.
<div id=”block2″><div id=”block2-title”>LIVE EVENTS</div>
<?php
echo do_shortcode(‘[events_list limit=”1″ category=6]#_EVENTIMAGE{145,90} </br>
<div class=”postname”>#_EVENTLINK</div>
<div class=”e-date”>#_EVENTDATES</br>#_EVENTTIMES</div><div class=”postexcerpt”>#_EVENTEXCERPT{20,…}</div>
<div class=”more-link” style=”margin-left: 10px”>More</div>
<div class=”tickets” style=”margin-left: 10px”>
#_ATT{Tickets}</div>[/events_list]’);?>
</div>
-
You may need to use some PHP code to do this. How are you setting the featured category for the homepage?
In the php above I have called for it to show one event from category 6 in the Live Events block and the rest is styling divs. I wondered if it was an else instruction I needed as below but it doesn’t work:
<div id=”block2″><div id=”block2-title”>LIVE EVENTS</div>
<?php
if (‘[events_list limit=”1″ category=6]’)) { echo do_shortcode(‘[events_list limit=”1″ category=6]#_EVENTIMAGE{145,90} </br>
<div class=”postname”>#_EVENTLINK</div>
<div class=”e-date”>#_EVENTDATES</br>#_EVENTTIMES</div><div class=”postexcerpt”>#_EVENTEXCERPT{20,…}</div>
<div class=”more-link” style=”margin-left: 10px”>More</div>
<div class=”tickets” style=”margin-left: 10px”>
#_ATT{Tickets}</div>[/events_list]’);
} else do_shortcode(‘[events_list limit=”1″]#_EVENTIMAGE{145,90} </br>
<div class=”postname”>#_EVENTLINK</div>
<div class=”e-date”>#_EVENTDATES</br>#_EVENTTIMES</div><div class=”postexcerpt”>#_EVENTEXCERPT{20,…}</div>
<div class=”more-link” style=”margin-left: 10px”>More</div>
<div class=”tickets” style=”margin-left: 10px”>
#_ATT{Tickets}</div>[/events_list]’); ?>
</div>I basically want to show a list of events from a specific category unless there aren’t any in that category then just show the next event regardless of what category it is in.
I think the problem is:
if ('[events_list limit="1" category=6]')) {
If there are no events in category 6, the shortcode will return ‘No events’, rather than nothing so you need to check whether or not that string exists to run your if/else statement.
I don’t quite understand, the current code shows ‘No Events’ so do I need to add another instruction? I tried it without ‘if’, saying show 3 events in category 78, ‘else’ show a single event but still not working:
<div id=”container” class=”js-masonry”data-masonry-options='{ “columnWidth”: 15, “itemSelector”: “.HM-event” }’>
<?php
echo do_shortcode(‘[events_list limit=”3″ category=78]<div class=”HM-event”>
<div class=”HM-pic”>#_EVENTIMAGE{300,0}</div>
<div class=”HM-eventnm”>#_EVENTLINK</div>
<div class=”HM-eventdt”>#_EVENTDATES</div>
<div class=”HM-venue”>
#_ATT{Venue}</div><div class=”tickets”>#_ATT{Tickets}</div>
</div>[/events_list]’);
} else { ?>
<?php
echo do_shortcode(‘[events_list limit=”1″]<div class=”HM-event”>
<div class=”HM-pic”>#_EVENTIMAGE{300,0}</div>
<div class=”HM-eventnm”>#_EVENTLINK</div>
<div class=”HM-eventdt”>#_EVENTDATES</div>
<div class=”HM-venue”>
#_ATT{Venue}</div><div class=”tickets”>#_ATT{Tickets}</div>
</div>[/events_list]’);?>
</div><!– #container –>
you can try to use $EM_Events::count();
eg.
$count = $EM_Events::count( array( 'scope' => 'future') );
Thanks, so where would I put that?
I think Angelo means to use
$count = $EM_Events::count( array( 'scope' => 'future') ); if ($count > 0) {
instead of your original if statement.
Thanks for the feedback, I am still having a little trouble with my code.
Thank you for your patience but if I strip it back just to get it clear in my head. Below calls three events in category 78<div id=”container”>
<?php
echo do_shortcode(‘[events_list limit=”3″ category=78]<div class=”HM-event”>
<div class=”HM-pic”>#_EVENTIMAGE{300,0}</div>
<div class=”HM-eventnm”>#_EVENTLINK</div>
<div class=”HM-eventdt”>#_EVENTDATES</div>
<div class=”HM-venue”>
#_ATT{Venue}</div><div class=”tickets”>#_ATT{Tickets}</div>
</div>[/events_list]’);
?></div><!– #container –>
I now want to add an instruction: if there are no events in category 78, just show the next event coming up. Do I add an if instruction afterwards. The below gives me the dreaded white screen:
<div id=”container”>
<?php
echo do_shortcode(‘[events_list limit=”3″ category=78]<div class=”HM-event”>
<div class=”HM-pic”>#_EVENTIMAGE{300,0}</div>
<div class=”HM-eventnm”>#_EVENTLINK</div>
<div class=”HM-eventdt”>#_EVENTDATES</div>
<div class=”HM-venue”>
#_ATT{Venue}</div><div class=”tickets”>#_ATT{Tickets}</div>
</div>[/events_list]’);?>
$count = $EM_Events::count( array( ‘scope’ => ‘future’) );
<?php
if ($count > 0) {
echo do_shortcode(‘[events_list limit=”1″]<div class=”HM-event”>
<div class=”HM-pic”>#_EVENTIMAGE{300,0}</div>
<div class=”HM-eventnm”>#_EVENTLINK</div>
<div class=”HM-eventdt”>#_EVENTDATES</div>
<div class=”HM-venue”>
#_ATT{Venue}</div><div class=”tickets”>#_ATT{Tickets}</div>
</div>
[/events_list]’);?></div><!– #container –>
I know I am getting in a muddle with this one, your help is really appreciated!x
This bit is definitely wrong:
[/events_list]');?> $count = $EM_Events::count( array( 'scope' => 'future') ); <?php if ($count > 0) {
You have opening and closing PHP tags when you shouldn’t have. This is correct:
[/events_list]'); $count = $EM_Events::count( array( 'scope' => 'future', 'category' => '78') ); if ($count > 0) {
I also added the category to the snippet above because you’ll need that too.
Thanks for your answer, the php is calling the events lists, so if I remove them I get the dreaded white screen:
The code below just shows two events list and ignores the $count instruction and shows that on the front end as copy?!<div id=”container”>
<?php
echo do_shortcode(‘[events_list limit=”3″ category=”78″]<div class=”HM-event”>
<div class=”HM-pic”>#_EVENTIMAGE{300,0}</div>
<div class=”HM-eventnm”>#_EVENTLINK</div>
<div class=”HM-eventdt”>#_EVENTDATES</div>
<div class=”HM-venue”>
#_ATT{Venue}</div><div class=”tickets”>#_ATT{Tickets}</div>
</div>
[/events_list]’);?>$count = $EM_Events::count( array( ‘scope’ => ‘future’, ‘category’ => ’78’) );
if ($count > 0) {<?php echo do_shortcode(‘[events_list limit=”3″]
<div class=”HM-event”>
<div class=”HM-pic”>#_EVENTIMAGE{300,0}</div>
<div class=”HM-eventnm”>#_EVENTLINK</div>
<div class=”HM-eventdt”>#_EVENTDATES</div>
<div class=”HM-venue”>
#_ATT{Venue}</div><div class=”tickets”>#_ATT{Tickets}</div>
</div>
[/events_list]’);?></div><!– #container –>
See my answer above. You are still opening and closing PHP tags when you shouldn’t.
[/events_list]');?>
needs to be
[/events_list]');
This is also wrong:
if ($count > 0) { <?php echo do_shortcode('[events_list limit="3"]
It should be:
if ($count > 0) { echo do_shortcode('[events_list limit="3"]
Thanks for your response, the php is calling the events lists, so if I remove them I get the dreaded white screen.
I have discovered the closing bracket was missing before the closing php tag at the end and although the white screen has gone back to normal, it appears nothing is being read from the $count line, does it need a preceding instruction maybe?
<div id=”container”>
<?php
echo do_shortcode(‘[events_list limit=”3″ category=”78″]<div class=”HM-event”>
<div class=”HM-pic”>#_EVENTIMAGE{300,0}</div>
<div class=”HM-eventnm”>#_EVENTLINK</div>
<div class=”HM-eventdt”>#_EVENTDATES</div>
<div class=”HM-venue”>#_ATT{Venue}</div>
<div class=”tickets”>#_ATT{Tickets}</div>
</div>
[/events_list]’);
$count = $EM_Events::count(array( ‘scope’ => ‘future’, ‘category’ => ’78’));
if ($count > 0){echo do_shortcode(‘[events_list limit=”3″]
<div class=”HM-event”>
<div class=”HM-pic”>#_EVENTIMAGE{300,0}</div>
<div class=”HM-eventnm”>#_EVENTLINK</div>
<div class=”HM-eventdt”>#_EVENTDATES</div>
<div class=”HM-venue”>#_ATT{Venue}</div>
<div class=”tickets”>#_ATT{Tickets}</div>
</div>
[/events_list]’); }
?>
</div><!– #container –>if you try this snippet
$count = EM_Events::count(array( 'scope' => 'future', 'category' => '78'));
- The topic ‘Calling a list of events’ is closed to new replies.