Needs a complete rewrite…
add_shortcode( 'categorypost', 'cat_post' );
function cat_post( $atts ) {
// attributes for shortcode
if ( isset( $atts['cat'] ) ) {$cats = $atts['cat'];} else {return;}
if ( isset( $atts['posts_per_page'] ) ) {$posts_per_page = $atts['posts_per_page'];} else {$posts_per_page = -1;}
// get the category posts
$category = get_category_by_slug( $cats );
if ( !is_object( $category ) ) {return;}
$args = array(
'cat' => $category->term_id,
'posts_per_page' => $posts_per_page
);
$posts = get_posts( $args );
// create the list output
if ( count( $posts ) > 0 ) {
foreach ( $posts as $post ) {
$link = get_permalink( $post->ID );
$title = $post->post_title;
$image = get_post_thumbnail( $post->ID,'thumbnail' );
$output .= '<div id="postrow-'.$post->ID.'" class="postrow">';
$output .= '<a class="postlink" href="'.$link.'">' . $image;
$output .= '<h5 class="posttitle">' . $title . '</h5></a></div>';
}
}
return $output;
}
Example shortcode usage: [categorypost cat="pant" posts_per_page="5"]