Custom Category Archive Pages

This topic contains 8 replies, has 2 voices, and was last updated by  Dario 11 years, 3 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #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
    #8419

    I also require the slug for the category for a calendar link that I wish to add to this page…

    Dario
    #8422

    The 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 for event-category or event-venue taxonomies. 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 cat and the function get_category() both relate specifically to WordPress’ default category taxonomy. For event taxonomies you’ll want the query variable event-category and the function get_term() (codex), specifying the taxonomy event-category as the second argument.

    However there’s an easier method still. From inside the event category template:

     $event_cat = get_queried_object().
    Stephen Harris
    #8426

    Thanks 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
    #8428

    This 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() at pre_get_posts to check that you are only altering the main query not every query on that page.

    Stephen Harris
    #8429

    Sweet, thanks.

    Dario
    #8431

    Actually 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
    #8432

    get_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
    #8435

    Thanks Stephen, works perfectly now.

    Dario
Viewing 9 posts - 1 through 9 (of 9 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.