admin管理员组文章数量:1416317
I have created a custom post_type called (event) and here's the code
// Register Custom Post Type Events
function custom_post_type_events() {
$labels = array(
'name' => _x( 'Events', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Event', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Events', 'text_domain' ),
'parent_item_colon' => __( '', 'text_domain' ),
'all_items' => __( 'All Events', 'text_domain' ),
'view_item' => __( 'View Event', 'text_domain' ),
'add_new_item' => __( 'Add New Event', 'text_domain' ),
'add_new' => __( 'New Event', 'text_domain' ),
'edit_item' => __( 'Edit Event', 'text_domain' ),
'update_item' => __( 'Update Event', 'text_domain' ),
'search_items' => __( 'Search events', 'text_domain' ),
'not_found' => __( 'No events found', 'text_domain' ),
'not_found_in_trash' => __( 'No events found in Trash', 'text_domain' ),
);
$args = array(
'label' => __( 'event', 'text_domain' ),
'description' => __( 'Events information pages', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'post-formats', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => admin_url() . '/images/ef-events-icon.png', // Icon Path
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'event', $args );
}
// Hook into the 'init' action
add_action( 'init', 'custom_post_type_events', 0 );
Then I have created a custom date meta_box for this event post_type, and here's the code
add_filter( 'cmb_meta_boxes', 'dt_person_meta_boxes' );
function dt_person_meta_boxes( $meta_boxes ) {
$meta_boxes[] = array(
'id' => 'dt_metabox',
'title' => 'Event Date and Time',
'pages' => array('event'),
'context' => 'side',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'Event Start Date',
'desc' => 'Pick the date of this event',
'id' => $prefix . 'event_starttextdate',
'type' => 'text_date'
),
array(
'name' => 'Event End Date',
'desc' => 'Pick the date of this event',
'id' => $prefix . 'event_endtextdate',
'type' => 'text_date'
),
array(
'name' => 'Event Time',
'desc' => 'Enter event time (eg. 05:00 p.m.)',
'id' => $prefix . 'event_time',
'type' => 'text',
'save' => true
)
)
);
return $meta_boxes;
}
My question is:
How to build a custom archive page for the custom post_type (event), that will sort events by the custom meta-box (event_starttextdate) ???
Example:
2013
- Event-1
- Event-2
2012
- Event-3
- Event-4
2011
- Event-5
- Event-6
- Event-7
- Event-8
Your help is appreciated, Thank you.
I have created a custom post_type called (event) and here's the code
// Register Custom Post Type Events
function custom_post_type_events() {
$labels = array(
'name' => _x( 'Events', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Event', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Events', 'text_domain' ),
'parent_item_colon' => __( '', 'text_domain' ),
'all_items' => __( 'All Events', 'text_domain' ),
'view_item' => __( 'View Event', 'text_domain' ),
'add_new_item' => __( 'Add New Event', 'text_domain' ),
'add_new' => __( 'New Event', 'text_domain' ),
'edit_item' => __( 'Edit Event', 'text_domain' ),
'update_item' => __( 'Update Event', 'text_domain' ),
'search_items' => __( 'Search events', 'text_domain' ),
'not_found' => __( 'No events found', 'text_domain' ),
'not_found_in_trash' => __( 'No events found in Trash', 'text_domain' ),
);
$args = array(
'label' => __( 'event', 'text_domain' ),
'description' => __( 'Events information pages', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'post-formats', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => admin_url() . '/images/ef-events-icon.png', // Icon Path
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'event', $args );
}
// Hook into the 'init' action
add_action( 'init', 'custom_post_type_events', 0 );
Then I have created a custom date meta_box for this event post_type, and here's the code
add_filter( 'cmb_meta_boxes', 'dt_person_meta_boxes' );
function dt_person_meta_boxes( $meta_boxes ) {
$meta_boxes[] = array(
'id' => 'dt_metabox',
'title' => 'Event Date and Time',
'pages' => array('event'),
'context' => 'side',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'Event Start Date',
'desc' => 'Pick the date of this event',
'id' => $prefix . 'event_starttextdate',
'type' => 'text_date'
),
array(
'name' => 'Event End Date',
'desc' => 'Pick the date of this event',
'id' => $prefix . 'event_endtextdate',
'type' => 'text_date'
),
array(
'name' => 'Event Time',
'desc' => 'Enter event time (eg. 05:00 p.m.)',
'id' => $prefix . 'event_time',
'type' => 'text',
'save' => true
)
)
);
return $meta_boxes;
}
My question is:
How to build a custom archive page for the custom post_type (event), that will sort events by the custom meta-box (event_starttextdate) ???
Example:
2013
- Event-1
- Event-2
2012
- Event-3
- Event-4
2011
- Event-5
- Event-6
- Event-7
- Event-8
Your help is appreciated, Thank you.
Share Improve this question edited Nov 30, 2013 at 11:06 efoula asked Nov 16, 2013 at 16:08 efoulaefoula 11 silver badge4 bronze badges 4 |1 Answer
Reset to default 0I just coded a workaround solution for this issue, if anyone is interested here is my solution:
This solution is using the Bootstrap Navs Option , so you need first to download Bootstrap and link it in your header or index file.
Please read the comments in the below code carefully to can understanding what is happened
The Solution (Code):
<?php
/*
Template Name: Archives Events
*/
echo '<div class="tabbable">';/* Start Bootstrap Tabs Parent Div (tabbable) */
$args = array(
'post_type' => 'event',/* Change with your custom post type name */
'meta_key' => 'event_starttextdate',/* Change with your custom date metabox (Event Start Date) */
'orderby'=> 'meta_value',
'order' => 'ASC',
);
/* Important Variables */
$emptyvalue = "";
$optionname = "optionname";
$the_query = new WP_Query($args);
/* Start Bootstrap Tabs ul */
echo '<ul class="nav nav-tabs">';
/* Query Posts (Events) */
$arr = array();
while ( $the_query->have_posts() ) : $the_query->next_post();
$id= $the_query->post->ID;
/* Get all dates of posts from the custom date metabox (Event Start Date) */
$evestdate = get_post_meta($id, 'event_starttextdate', true);
/* Strtime the year from the results */
$this_month = strtotime($evestdate);
$cuseveyear = date( 'Y', $this_month );
$archiveyear = $cuseveyear;
$archiveyears = $archiveyear;
/* Remove duplicated years */
if(!in_array($archiveyear, $arr)){
array_push($arr, $archiveyear);
$get_archiveyear = $_GET['institution-archiveyear'];
/* For each year do the following */
foreach( (array) $archiveyears as $archiveyear ) {
echo '<li>';/* create li and a href for each year */
echo "<a class='archiveyear' href='#".$archiveyear."' data-toggle='tab'>"; /* href is equal to the result year */
echo $archiveyear ;
echo '</a>';
echo '</li>';
}
}
endwhile;
echo '</ul>';
/* End Bootstrap Tabs ul */
?>
<?php
$args = array(
'post_type' => 'event',/* Change with your custom post type name */
'meta_key' => 'event_starttextdate',/* Change with your custom date metabox (Event Start Date) */
'orderby'=> 'meta_value',
'order' => 'ASC',
);
/* Important Variables */
$emptyvalue = "";
$optionname = "optionname";
$the_query = new WP_Query($args);
/* Start Bootstrap Tab content div */
echo '<div class="tab-content">';
$arr = array();
/* Query Posts (Events) */
while ( $the_query->have_posts() ) : $the_query->next_post();
$id= $the_query->post->ID;
/* Get all dates of posts from the custom date metabox (Event Start Date) */
$evestdate = get_post_meta($id, 'event_starttextdate', true);
/* Strtime the year from the results */
$this_month = strtotime($evestdate);
$cuseveyear = date( 'Y', $this_month );
$archiveyear = $cuseveyear;
$archiveyears = $archiveyear;
/* Remove duplicated years */
if(!in_array($archiveyear, $arr)){
array_push($arr, $archiveyear);
$get_archiveyear = $_GET['institution-archiveyear'];
/* For each year do the following */
foreach( (array) $archiveyears as $archiveyear ) {
/* create Bootstrap div tab-pane */
echo "<div class='tab-pane' id='".$archiveyear."'>";
$argss = array(
'post_type'=> 'event',
);
$the_query = new WP_Query( $argss );
while ( $the_query->have_posts() ) : $the_query->the_post();
/* Get all dates of posts from the custom date metabox (Event Start Date) */
$evestdate_single = get_post_meta( $post->ID, 'event_starttextdate', true );
/* Strtime the year from the results */
$this_month_single = strtotime($evestdate_single);
$cuseveyear_single = date( 'Y', $this_month_single );
$cuspermalink = get_post_permalink($id);
/* If the result year is equal to the archive year do the following */
if ($cuseveyear_single == $archiveyear) {
echo '<p style="border-bottom:1px solid #DDDDDD;padding:0;padding-bottom:5px;margin-left:100px;">';
echo "<a href='".$cuspermalink."'>";
echo the_title();
echo '</a>';
echo '</p>';
}
endwhile;
} echo '</div>';/* End Bootstrap div tab-pane */
}
endwhile;
echo '</div>';/* End Bootstrap Tab content div */
echo '</div>';/* End Bootstrap Tabs Parent Div (tabbable) */
?>
本文标签: Yearly Archive from a custom date metabox (Event Start Date)
版权声明:本文标题:Yearly Archive from a custom date metabox (Event Start Date) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745243931a2649469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
archive-event.php
. – Pat J Commented Nov 20, 2013 at 19:55