Skip to content

Instantly share code, notes, and snippets.

@EranSch
Created August 6, 2014 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EranSch/4a04469a95796281ccc1 to your computer and use it in GitHub Desktop.
Save EranSch/4a04469a95796281ccc1 to your computer and use it in GitHub Desktop.
Custom Post Type Query, Groupped by Categories
// Add Shortcode
function equipment_list_shortcode( $atts ) {
// Attributes
extract( shortcode_atts(
array(
'trip' => '',
), $atts )
);
$equipment_table = "";
// Code
if ( !isset( $trip ) ) {
return '';
}else{
$equipment_table .= "<script type='text/javascript' src='" . plugins_url( 'jquery.printElement.min.js' , __FILE__ ) . "'></script>";
$equipment_table .= "<style type='text/css'>
.equipmentTable table td {
vertical-align: top;
}
.name {
width: 15%;
}
.guidesPick {
width: 30%;
}
.items li {
list-style: none;
}
.items {
margin-left: 0 !important;
}
@font-face {
font-family: 'print';
src:url('" . plugins_url( 'fonts/icomoon.eot' , __FILE__ ) . "');
src:url('" . plugins_url( 'fonts/icomoon.eot?#iefix' , __FILE__ ) . "') format('embedded-opentype'),
url('" . plugins_url( 'fonts/icomoon.ttf' , __FILE__ ) . "') format('truetype'),
url('" . plugins_url( 'fonts/icomoon.woff' , __FILE__ ) . "') format('woff'),
url('" . plugins_url( 'fonts/icomoon.svg' , __FILE__ ) . "#icomoon') format('svg');
font-weight: normal;
font-style: normal;
}
.icon-print {
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-decoration: none;
float:right;
position: relative;
top:25px;
}
.icon-print:after {
font-family: 'print' ;
content: '\e600';
font-size: 2em;
}
</style>";
$equipment_table .= "<div class='equipmentTable'>";
$equipment_table .= "<a class='icon-print' href='#' onclick=\"javascript:jQuery('.equipmentTable').printElement({overrideElementCSS:[{href:'" . plugins_url( 'print.css' , __FILE__ ) . "',media:'print'}],leaveOpen:true,})\"></a>";
$post_type = 'equipment';
$tax = 'gear';
$tax_terms = get_terms($tax);
foreach ($tax_terms as $tax_term){
// WP_Query arguments
$args = array (
'post_type' => $post_type,
'trip' => $trip,
'pagination' => false,
'ignore_sticky_posts' => true,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'orderby' => 'menu_order',
'order' => 'ASC'
);
// The Query
$equip_query = new WP_Query( $args );
// The Loop
if ( $equip_query->have_posts() ) {
$equipment_table .= "<h4>" . $tax_term->name . "</h4>";
$equipment_table .= '<table border="0" bordercolor="" width="100%" cellpadding="1" cellspacing="3">';
$equipment_table .= "<tr><th class='name'>Gear</th><th class='description'>Description</th><th class='guidesPick'>Guide's Pick</th></tr>";
while ( $equip_query->have_posts() ) {
$equip_query->the_post();
$equipment_table .= "<tr>";
$equipment_table .= "<td class='name'>" . get_the_title() . "</td>";
$excerpt = get_the_excerpt();
if( $excerpt != '' ){
$equipment_table .= "<td class='description'>" . $excerpt . "</td>";
}else{
$equipment_table .= "<td class='description'>" . get_field('description') . "</td>";
}
if( get_field('products') ){
$equipment_table .= "<td class='guidesPick'><ul class='items'>";
while(has_sub_field('products')){
if( get_sub_field('link') ){
$equipment_table .= "<li><a target='_blank' href='" . get_sub_field('link') . "'>" . get_sub_field('name') . "</a></li>";
}else{
$equipment_table .= "<li>" . get_sub_field('name') . "</li>";
}
}
$equipment_table .= "</ul></td>";
}else{
$equipment_table .= "<td></td>";
}
$equipment_table .= "</tr>";
}
$equipment_table .= "</table>";
}
}
}
$equipment_table .= "</div>";
wp_reset_postdata();
return $equipment_table;
}
add_shortcode( 'equipment_list', 'equipment_list_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment