Custom Category Archive Pages
WordPress Event Management, Calendars & Registration › Forums › General Question › Custom Category Archive Pages
This topic contains 8 replies, has 2 voices, and was last updated by Dario 11 years, 11 months ago.
-
AuthorPosts
-
November 28, 2013 at 7:01 am #8418
Hi Stephen,
Trying to create custom category pages but have run into some issues with the number of events shown per page.
The number of events shown per page for my news posts is 3 (set in the WordPress settings), however on my event achieve page I’m using the eo_get_events() function to display all the events on one page.
Now when I come to my custom category pages, only 3 events are displayed (due to the global setting), however I am unsure of how I can edit the
<?php while ( have_posts() ) : the_post(); ?>line in order to display all events.Alternatively I tried the eo_get_events() function, however it seems that the cat variable doesn’t ork on eo category archive pages as it does on regular archive pages:
$cat = get_category( get_query_var( 'cat' ) ); // Doesn't work $cat_slug = $cat->slug; $events = eo_get_events(array( 'numberposts'=>-1, 'events_start_after'=>'now', 'showpastevents'=>false, //Deprecated 'tax_query'=>array( array( 'taxonomy'=>'event-category', 'operator' => 'IN', 'field'=>'slug', 'terms'=>array( $cat_slug ) )), ));Do you know a way to get around this issue?
Thanks
Dario
November 28, 2013 at 8:52 am #8419I also require the slug for the category for a calendar link that I wish to add to this page…
Dario
November 28, 2013 at 12:03 pm #8422The best way to do this would be to intercept the query before its performed, rather than re-querying in the template. (Otherwise you can end up breaking pagination, and you’re doubling the work the server has to do). This also keeps you template clean.
To change the number of events per page:
add_action( 'pre_get_posts', 'dario_change_events_per_page' ); function dario_change_events_per_page( $query ){ //Just in case EO is uninstalled check it exists before using its functions if( !defined( 'EVENT_ORGANISER_DIR' ) ) return; //If its a main query for events, set posts_per_age to -1 (unlimited) if( eventorganiser_is_event_query( $query ) && $query->is_main_query() ){ $query->set( 'posts_per_page', -1 ); } }There are other conditionals you can use e.g.
is_tax()to check forevent-categoryorevent-venuetaxonomies. Or for checking date-based event queries,eo_is_event_archive(), where you can optionally specify ‘day’, ‘month’ or ‘year’ (http://codex.wp-event-organiser.com/function-eo_is_event_archive.html).With regards to
$cat = get_category( get_query_var( 'cat' ) ); `the query variable
catand the functionget_category()both relate specifically to WordPress’ default category taxonomy. For event taxonomies you’ll want the query variableevent-categoryand the functionget_term()(codex), specifying the taxonomyevent-categoryas the second argument.However there’s an easier method still. From inside the event category template:
$event_cat = get_queried_object().Stephen Harris
November 28, 2013 at 12:49 pm #8426Thanks Stephen 🙂
Just another slightly related thing, since the is_category() function will also not work for this, how would I best create an if else statement?
if ( event-category-archive )
else if ( event-tag-archive )
else if ( event-venue-archive )
else if ( event-archive )
else // regular posts
Dario
November 28, 2013 at 12:55 pm #8428This should do the trick:
if ( is_tax( 'event-category' ) ){ //Event category page }elseif( is_tax( 'event-tag' ) ){ //Event venue page }elseif( is_tax( 'event-venue' ) ){ //Event venue page }elseif( is_post_type_archive( 'event' ) ){ //Event archive page (including day/month/year event pages) }else{ // Everything else }Typically you want to make sure
$query->is_main_query()atpre_get_poststo check that you are only altering the main query not every query on that page.Stephen Harris
November 28, 2013 at 12:59 pm #8429Sweet, thanks.
Dario
November 28, 2013 at 2:03 pm #8431Actually I seem to be getting some errors.
<?php $category_slug = get_queried_object(); echo "Slug: "; echo $category_slug; // Stops loading page past this point (no matter where I put it within the category archive page) ?>.
// This also doesn't seem to be valid, Stops loading page past this point if ( is_tax( 'event-venue' ) ):I’m trying to create a calendar feed subscribe sidebar widget : http://pastebin.com/uHETDz86
Dario
November 28, 2013 at 2:09 pm #8432get_queried_object()returns the object, not the slug,//In an event category / venue template: $term = get_queried_object(); $slug = $term->slug; $name = $term->name;In the pastebin there are a couple of errors on the ‘placeholder’ lines too:
$placeholder = 'category="' . $cat_slug; . '"'has a semi-colon after
$cat_slug, it should instead be:$placeholder = 'category="' . $cat_slug . '"'Stephen Harris
November 28, 2013 at 11:04 pm #8435Thanks Stephen, works perfectly now.
Dario
-
AuthorPosts