Get Shortcode Attributes

I figured it out. With the shortcode set as [camp_posts type=”academic, sports”] the code that does the trick is this:

function camp_posts_function($atts) {//Creates shortcode [camp_posts type="academics, sports"]
$atts=shortcode_atts(
        array(
            'type' => 'nothing',
        ), $atts, 'camp_posts');
$ShortcodeAtts=esc_attr($atts['type']);//Puts "type" attribute into a variable
$ShortcodeAttsArray = explode(',', $ShortcodeAtts);

$IDout = array();//Initializes array
foreach($ShortcodeAttsArray as $slug) {
    array_push($IDout, get_category_by_slug($slug)->term_id);//Gets category ID of each category slug name
    }
$CatIDs= implode(',', $IDout);//Puts all category IDs into variable $CatIDs with a commas between

$camp_posts_bynumber = new WP_Query(array ('cat' => $CatIDs));

if( $camp_posts_bynumber->have_posts() ):

Leave a Comment