I am making a custom shortcode which basically is just meant to return my custom post type, here’s my code:
function shortcode_slider($atts, $content=null){
extract(shortcode_atts( array('id' => ''), $atts));
$return = $content;
$return .= query_posts( array( 'post_status' => 'publish' , 'post_type' => 'slider' ) );
return $return;
}
add_shortcode('slider', 'shortcode_slider');
The shortcode works ok apart from one thing – when it returns all of the posts it also returns “array” at the top of the list – any idea why this would happen?
Also, I want to be able to use the “id” input to be able to specify a category, e.g.
$return .= query_posts( array( 'post_status' => 'publish' , 'post_type' => 'slider', 'category' => $id ) );
but I am unsure of the correct syntax for this.
Any help is much appreciated.
First of all, don’t use
query_posts.Please, check:
In a shortcode case, I’d go with
get_posts. It will return what you need and won’t mess with the loop.It is worth noting that you don’t really need to extract the attributes, using
$atts['id']does the work.Follows a working shortcode, see comments:
Shortcode applied in a default post type:
Custom post types:
Result:
Useful links: