Created
August 8, 2013 16:23
-
-
Save sevenspark/6186138 to your computer and use it in GitHub Desktop.
UberMenu Post Grid Shortcode & Styling
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
<?php | |
/** ADD EVERYTHING BELOW THIS LINE TO YOUR CHILD THEME'S FUNCTIONS.PHP **/ | |
function ubermenu_post_grid( $atts ){ | |
//we're going to need access to the UberMenu object for image functionality | |
global $uberMenu; | |
//Merge user-provided attributes with default values | |
//and extract into individual variables | |
extract(shortcode_atts(array( | |
'num' => 4, //maximum number of posts to retrieve | |
'grid' => 4, //number of columns per row | |
'category' => '', //optional category to retrieve posts from | |
'img' => 'on', //'on' or 'off' to display the image or not | |
'img_width' => 220, //image width in pixels | |
'img_height'=> 120, //image height in pixels | |
'excerpt' => 'off', //'on' or 'off' to display the excerpt | |
'default_img' => false, //URL of a default image to display when no featured image is available | |
'offset' => 0, //offset for get_posts() (number of posts to skip) | |
), $atts)); | |
//Setup default query arguments | |
$query_args = array( | |
'numberposts' => $num, | |
'offset' => $offset, | |
'suppress_filters' => false | |
); | |
//If the user has provided a category, parse it into the query args | |
if( !empty( $category ) ){ | |
//Handle numeric category values | |
if(is_numeric($category)){ | |
$query_args['category'] = $category; | |
} | |
//Handle category names as well | |
else $query_args['category_name'] = $category; | |
} | |
//Retrieve matching posts from the database | |
$posts = get_posts( $query_args ); | |
//Wrap our grid in an unordered list container | |
$html = '<ul class="uber-post-grid uber-post-grid-'.$grid.'">'; | |
//Loop through each post and output the image, title, and excerpt | |
foreach( $posts as $post ){ | |
//Each post becomes a list item | |
$html.= '<li class="uber-post-grid-item post-'.$post->ID.'">'; | |
//We will wrap the entire item contents with the post's permalink (note anchors can be block level elements in HTML5) | |
$html.= '<a href="'.get_permalink( $post->ID ).'">'; | |
//Get the image at the right size and append it | |
$image = ''; | |
if($img == 'on'){ | |
$image = $uberMenu->getPostImage($post->ID, $img_width, $img_height, $default_img ); | |
$html.= $image; | |
} | |
//Add the post title | |
$html.= '<h5>'.$post->post_title.'</h5>'; | |
//Add the excerpt | |
if($excerpt == 'on') | |
$html.= '<span class="uber-post-grid-excerpt">'.apply_filters( 'get_the_excerpt', $post->post_excerpt ).'</span>'; | |
$html.= '</a>'; | |
$html.= '</li>'; | |
} | |
$html.= '</ul>'; | |
//Return the entire list | |
return $html; | |
} | |
add_shortcode( 'ubermenu-post-grid' , 'ubermenu_post_grid' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment