Skip to content

Instantly share code, notes, and snippets.

@dbasilioesp
Created September 3, 2015 16:25
Show Gist options
  • Save dbasilioesp/2ea86fcf576823cbe697 to your computer and use it in GitHub Desktop.
Save dbasilioesp/2ea86fcf576823cbe697 to your computer and use it in GitHub Desktop.
Listagem de posts do wordpress de forma customizada.
// Shortcode Agenda
function agenda_shortcode() {
//@ini_set('display_errors', 0);
$html = "";
$week = array('segunda', 'terca', 'quarta', 'quinta', 'sexta', 'sabado', 'domingo');
foreach ($week as &$day) {
// WP_Query arguments
$args = array (
'post_type' => array( 'agenda-item' ),
'category_name' => $day,
);
// The Query
$query = new WP_Query( $args );
if ($query->have_posts()) {
$category_id = get_cat_ID( $day );
$html .= "<div class='agenda-day'>".get_cat_name( $category_id )."</div>";
$oddEver = 'odd';
while ($query->have_posts()) {
$image_id = null;
$image_link = null;
$image_title = null;
$agendaItemClass = "";
$oddEver = $oddEver == 'odd' ? 'ever' : 'odd';
$agendaItemClass .= $oddEver;
$query->the_post();
if ( has_post_thumbnail($post->ID) ) {
$image_id = get_post_thumbnail_id($post->ID);
$image_link = wp_get_attachment_image_src($image_id,'full');
$image_title = esc_attr( get_the_title($post->ID) );
$agendaItemClass .= " has-thumbnail";
}
$html .= "<div class='container-fluid agenda-item ".$agendaItemClass."'>";
$html .= "<div class='row nospace'>";
if ($image_id) :
$html .= "<div class='col-md-3 hidden-xs hidden-sm'>";
$html .= "<img src='".$image_link[0]."' alt='".$image_title."' class='agenda-thumbnail' />";
$html .= "</div>";
$html .= "<div class='col-sm-12 col-md-9 agenda-content-wrap'>";
$html .= "<div class='table-center'>";
$html .= "<h3 class='agenda-title'>".$query->post->post_title."</h3>";
$html .= "<p class='agenda-content'>".$query->post->post_content."</p>";
$html .= "</div>";
$html .= "</div>";
else :
$html .= "<div class='col-md-12 agenda-content-wrap'>";
$html .= "<div class='table-center'>";
$html .= "<h3 class='agenda-title'>".$query->post->post_title."</h3>";
$html .= "<p class='agenda-content'>".$query->post->post_content."</p>";
$html .= "</div>";
$html .= "</div>";
endif;
$html .= "</div>";
$html .= "</div>";
}
}
/* Restore original Post Data */
wp_reset_postdata();
}
return $html;
}
add_shortcode( 'agenda', 'agenda_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment