function get_gifts_posts($params, $content){
$default_params = array(
'post_type' => 'post',
'order' => 'desc',
'orderby' => 'date',
'posts_per_page'=> 5,
);
if (isset($params['type']) && !empty($params['type']) && post_type_exists($params['type']))
$params['post_type'] = $params['type'];
if (isset($params['taxonomy']) && !empty($params['taxonomy']) && taxonomy_exists($params['taxonomy'])
&& isset($params['slug']) && !empty($params['slug']))
$params[$params['taxonomy']] = $params['slug'];
foreach(array('type', 'slug', 'taxonomy') as $key)
if (isset($params[$key]))
unset($params[$key]);
$params = wp_parse_args($params, $default_params);
$transient_hash="get_gifts_posts_".substr(md5(serialize($params)),0,10);
if (false === ($html = get_transient($transient_hash))){
add_filter('query', 'q');
global $request_sql;
// init
$html="";
$rp = new WP_Query($params);
// $html .= '<h2>START</h2>';
// $html .= '<pre>'.var_export($params, 1).'</pre>';
// $html .= $request_sql.'<hr>';
if ($rp->have_posts()){
$html .= '<ul>';
while ( $rp->have_posts() ) {
$rp->the_post();
$html .= "<li>";
if (($thumb_id = get_post_thumbnail_id())){
$thumb = wp_get_attachment_image_src($thumb_id, 'panel-small', false);
$html .= "<a href="https://wordpress.stackexchange.com/questions/135603/.get_permalink()."><div id=\"calimgf\" style=\"background:url({$thumb['0']}) no-repeat left top;\">";
}
$html .= "<span class=\"calendate\" >".get_field('fl_price_ex')."</span>";
$html .= "<span class=\"calenhead\" >".get_field('fl_title')."</span>";
$html .= "</div></a></li>";
}
$html .= '</ul>';
}
if (!empty($html))
set_transient($transient_hash, $html, 60);
}
return $html;
}
How can I specify the number of posts I would like to display as a parameter in the shortcode?
Right now I am showing 5 posts by default 'posts_per_page'=> 5,
If the user doesn’t specify the number of posts then the function should continue to display 5 posts by default.
1 Answer
There is a basic shortcode example in the Codex.
function bartag_func( $atts ) {
extract( shortcode_atts( array(
'foo' => 'no foo',
'baz' => 'default baz'
), $atts ) );
return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );
The array is the shortcode defaults and is used to fill in for any attribute that isn’t specfically passed in the shortcode parameters. Applied to your code it would look like:
function get_gifts_posts($atts, $content){
extract( shortcode_atts( array(
'post_type' => 'post',
'order' => 'desc',
'orderby' => 'date',
'posts_per_page'=> 5,
), $atts ) );
// function truncated
// but you may now use $post_type, $order, $orderby and $posts_per_page variables
}
add_shortcode('super_awesome_shortcode', 'get_gifts_posts' );
Update
The shortcode would now be used like so:
[super_awesome_shortcode posts_per_page="10"]
where the $posts_per_page would be equal to 10
or
[super_awesome_shortcode]
where $posts_per_page would be equal to the default, 5.
shortcode_atts()
combines user shortcode attributes with known attributes and fills in defaults when needed.