Last active
December 28, 2015 00:18
-
-
Save chssweb/7412057 to your computer and use it in GitHub Desktop.
Shortcode code to display custom post by date range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fellows_shortcode($atts) | |
{ | |
// Extract Shortcode Attributes | |
extract(shortcode_atts( | |
array( | |
'range' => 'all', | |
), $atts) | |
); | |
// Set up date ranges for query | |
$datetime = new DateTime(); | |
$mq_operator = array(); | |
if ($range == "former") { | |
$start = '19800101'; // 1st Jan 1980 | |
$end = $datetime->sub(new DateInterval('P1D'))->format('Ymd'); // -1 day | |
$mq_operator[0] = '>='; | |
$mq_operator[1] = '<='; | |
} elseif ($range == "future") { | |
$start = $datetime->add(new DateInterval('P1D'))->format('Ymd'); // +1 day | |
$end = '21130101'; | |
$mq_operator[0] = '>='; | |
$mq_operator[1] = '<='; | |
} else { | |
$start = $end = $datetime->format('Ymd'); | |
$mq_operator[0] = '<='; | |
$mq_operator[1] = '>='; | |
} | |
// Put everything into the WP_Query attributes array | |
// Currently set to order by lastname in descending order | |
$atts = array( | |
'post_status' => 'publish', | |
'post_type' => array('fellow'), | |
'posts_per_page' => 3, | |
'meta_key' => 'fellow_lname', | |
'orderby' => 'meta_value', | |
'order' => 'DESC', | |
'meta_query' => array( | |
array( | |
'key' => 'fellow_start_date', | |
'value' => $start, | |
'type' => 'date', | |
'compare' => $mq_operator[0] | |
), | |
array( | |
'key' => 'fellow_end_date', | |
'value' => $end, | |
'type' => 'date', | |
'compare' => $mq_operator[1] | |
) | |
) | |
); | |
// In case I ever decide to have paging I will include this | |
$atts['paged'] = (get_query_var('paged')) ? get_query_var('paged') : 1; | |
// The Query using WP_Query | |
// http://codex.wordpress.org/Class_Reference/WP_Query | |
$wp_query = null; | |
$wp_query = new WP_Query($atts); | |
// Declare some variables | |
$output = ''; | |
$count = 0; | |
// Number of fellows returned - may use | |
$post_count = $wp_query->found_posts; | |
/* The Loop */ | |
if ($wp_query->have_posts()) { | |
while ($wp_query->have_posts()) { | |
$count++; | |
$wp_query->the_post(); | |
// Using a simple {{}} templating system to keep things tidy | |
$content = '<li>{PHOTO} | |
<a href="{PERMALINK}">{TITLE}</a> | |
<span class="flist_position"><a href="{PERMALINK}">{POSITION}</a></span> | |
<span class="flist_date">{START_DATE}{END_DATE}</span> | |
<span class="flist_institution">{INSTITUTION}</span> | |
</li>'; | |
// Get the required fields in $parameters to fill the content template | |
$parameters = array( | |
'PERMALINK' => get_permalink(), | |
'TITLE' => get_the_title(), | |
'POSITION' => get_field('fellow_position'), | |
'PROJECT' => get_field('fellow_ptitle'), | |
'INSTITUTION' => get_field('fellow_institution'), | |
// add here more... | |
); | |
// Dates are a special case as require to be converted | |
// from YYmmdd to human readable format | |
if (get_field('fellow_start_date')) { | |
$sdate = DateTime::createFromFormat('Ymd', get_field('fellow_start_date')); | |
} | |
if (get_field('fellow_end_date')) { | |
$edate = DateTime::createFromFormat('Ymd', get_field('fellow_end_date')); | |
} | |
if ($sdate) { | |
$parameters['START_DATE'] = $sdate->format('M Y'); | |
} | |
if ($edate) { | |
$parameters['END_DATE'] = ' - ' . $edate->format('M Y'); | |
} | |
// Another special case: Photograph, as it requires more code to pull the content | |
if (get_field('fellow_photo')) { | |
$attachment_id = get_field('fellow_photo'); | |
$size = "thumbnail"; // (thumbnail, medium, large, full or custom size) | |
$parameters['PHOTO'] = wp_get_attachment_image($attachment_id['id'], $size); | |
} | |
// The template engine - replaces {{}} with appropriate $parameters[] value | |
$finds = $replaces = array(); | |
foreach ($parameters as $find => $replace) { | |
$finds[] = '{' . $find . '}'; | |
$replaces[] = $replace; | |
} | |
$output .= str_replace($finds, $replaces, $content); | |
} | |
// Wrap in UL and add classes | |
$output = '<ul class="fellows fellows_' . $range . '">' . $output . '</ul>'; | |
return $output; | |
} | |
} | |
add_shortcode('fellows', 'fellows_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment