# oddly the wp api has no category getter by name
# but we need it since the only function that gives the current category returns the name (not id/slug for which getters are available)
function get_category_by_name($name) {
$categories = get_categories();
foreach($categories as $c) {
if ($c->name == $name)
return $c;
}
return false;
}
function get_date($post, $field_name = NULL) {
$custom_fields = get_post_custom($post->ID);
$date = ($field_name && isset($custom_fields[$field_name])) ? implode("
", $custom_fields[$field_name]) :
isset($custom_fields["Date"]) ? implode("
", $custom_fields["Date"]) : get_the_time("j/n/Y, h:i", $post);
return $date;
}
function parse_date($date) {
$parts = array();
# ie. 23/09/2013, 10.00-13.00
if (preg_match('/^\s*(\d+)\/(\d+)\/(\d+)[, ]+(\d+)[:.](\d+).*/', $date, &$parts)) {
# h, m, s, M, D, Y, dst
$timestamp = mktime($parts[4], $parts[5], 0, $parts[2], $parts[1], $parts[3], 0);
return $timestamp;
}
return -1;
}
function sort_by_custom_field_date($p1, $p2) {
return ($p1->__sortstamp >= $p2->__sortstamp) ? 1 : -1;
}
# get the current category
$name = single_cat_title("", false);
$category = get_category_by_name($name);
#print_r($category);
# for categories
$debug = 1;
if ($debug) { ?>
Showing events in category: (id = , slug = )
}
# only show summer/autumn events
$summer = parse_date("1/07/2013, 10:00");
$hide_summer2013 = true;
$hide_autumn2013 = false;
# get the posts for said category, and sort them by a custom field (event date)
# default to post date if not set
$posts = get_posts(array("posts_per_page" => 100, "category" => $category ? $category->cat_ID : ""));
$posts2 = array();
foreach ($posts as $post) {
$date = get_date($post, "Sortdate");
$post->__sortdate = $date;
$post->__sortstamp = parse_date($date);
$post->__customclass = "";
$post_categories = wp_get_post_categories($post->ID);
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[$c] = array( 'id' => $c, 'name' => $cat->name, 'slug' => $cat->slug );
}
$post->__cats = array_values($cats);
$cats_s = array();
foreach (array_values($cats) as $c) {
array_push($cats_s, $c['name'] . "(" . $c['id'] . ")");
}
$post->__cats_s = implode(",
", $cats_s);
$custom_fields = get_post_custom($post->ID);
if (isset($custom_fields["Date2"])) {
$post->__displaydate = implode("
", $custom_fields["Date2"]);
$post->__customclass = "date2";
} else {
$post->__displaydate = date("j. n. Y", $post->__sortstamp);
}
if ($hide_summer2013 && $summer > $post->__sortstamp) {
continue;
} else if ($hide_autumn2013 && $summer < $post->__sortstamp) {
continue;
} else {
array_push(&$posts2, $post);
}
}
usort(&$posts2, "sort_by_custom_field_date");
?>
Sorry, no posts match your criteria.