passing multiple parents value into a shortcode

I need to pass a few values into the parent attribute of the wp_query. However I’m using a shortcode, so, If I pass a single value works perfect, but if I need to send multiple values it breaks, would be a great holiday gift if some1 could help me with it.

By default, it works with the current post ID $thePostID, however If I need to retrieve the parent I’m using the parent attribute. Take a look at my code.

 function andrew_child_loop_shortcode_new ( $atts ) {
      global $post; $thePostID = $post->ID;
      extract ( shortcode_atts (array (
        'posts' => 20,
        'parent' => $thePostID,
        'exclude' => array(0)
      ), $atts ) );
      ///////$atts[ 'parent' ] = explode( "," $atts[ 'parent' ] );
      ///////extract( $atts );
      $output="<div class="clear"></div>";
         $args = array(
            'orderby' =>  'menu_order',
            'order' => 'ASC',
            'post_parent' => $parent,
            'post_type' => 'page',
            'post__not_in' =>  array($exclude),
            'posts_per_page' => $posts
           );
      wp_reset_query();
      $i=0;
      $andrew3_query = new  WP_Query( $args );
        $output .= '<div id="multiple-childs" class="grid_8">';
      while (  $andrew3_query->have_posts()) { $i++ ; $andrew3_query->the_post();
        $output .= '<div id="child-results-multiple" class="grid_2 omega result-'.$i.'">';
        if(has_post_thumbnail()):;
        $output .= '<a href="'.
                    get_permalink().
                    '" a>'.
                    get_the_post_thumbnail(get_the_ID(), 'blog-post-carousel').
                    '</a>';
        else :;
        $output .= '<a href="'.
                    get_permalink().
                    '" a>'.
                      '<img src="'.
                       get_bloginfo( "template_url" ).
                       '/images/theme/no-image.png" width=115 height=115> </a>';

        endif;
        $output .=

               '<h4>'.
               get_the_title().
               '</h4>'.
               '<p>'.
               get_the_excerpt().
               '</p>'.
               '</div>';

      }
      wp_reset_query();
      $output .= '</div>';
      return $output;
    }
    add_shortcode('display_childs_multiple', 'andrew_child_loop_shortcode_new');  

I’m calling the parent this way [display_childs_multiple parent=”10,15,20″]

1 Answer
1

If you’re passing in a comma-delimited list of parent IDs when you call the shortcode, it’s pretty easy to turn this to an array.

For example, using [scname parent="5,10,15"]:

function andrew_child_loop_shortcode_new ( $atts ) {
    global $post; $thePostID = $post->ID;

    $atts = shortcode_atts( array(
        'posts'  => 20,
        'parent' => $thePostID
        ), $atts );

    // Turn the 'parent' parameter into an array
    $atts[ 'parent' ] = explode( ",", $atts[ 'parent' ] );

    extract( $atts );

    // ... now do whatever you were going to do ...

}

Now your $parent variable is an array of the post IDs passed in. If only one was passed in, it’s array with a single element. If you passed in multiple IDs, then you’ll have an array with multiple values.

How you construct your queries and move on from here is entirely up to you …

Leave a Comment