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]))

        $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() ) { 
                    $html .= "<li>";

                    if (($thumb_id = get_post_thumbnail_id())){
                        $thumb = wp_get_attachment_image_src($thumb_id, 'panel-small', false);
                        $html .= "<a href=".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.


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' );


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



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.

| improve this answer | |
  • There can only be one hook for each shortcode. Which means that if another plugin has a similar shortcode, it will override yours or yours will override theirs depending on which order the plugins are included and/or ran. I am not sure how to apply this to my example here? Is there not a simpler way to do this by just replacing the number "5" with a variable ? – JoaMika Feb 22 '14 at 23:04
  • I don't understand what you are asking in this comment, because I think what I've posted answers your original question. The number "5" is the default. It can be set modified the shortcode [super_awesome_shortcode posts_per_page="10"] and then $posts_per_page will be equal to 10. That is the beauty of shortcode_atts(). If you are afraid of conflict with another shortcode of the same name, you can always give your shortcode a more specific name that is unlikely to ever conflict. – helgatheviking Feb 22 '14 at 23:16
  • I see! it seems to work if i specify posts_per_page="10"... If I don't specify anything then it shows all posts instead of 5 but I guess I can live with that :) – JoaMika Feb 22 '14 at 23:23
  • Well $posts_per_page should equal 5 if you don't specify anything in the shortcode. Therefore it must be a matter of how you structure the rest of your function... which probably needs some adjustments (like ditching wp_parse_args since it is redundant). new WP_Query( 'posts_per_page' => $posts_per_page );. Also, I'd recommend against using transients until you get your code sorted as you won't see any changes. – helgatheviking Feb 22 '14 at 23:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.