My problem is that I can't access the $cat_slug
variable inside the [display-posts]
short-code. The idea is to use that variable to dynamically pick post categories via the array returned from get_the_category();
I thought it was a scoping issue but there's no function present. Do for loops have a scope that I'm not aware of? Any help is greatly appreciated as I'm fairly new to php! <3
Here's the snippet I'm working with; I've left in my sudo code to illustrate my thought process.
//gets an array of objects containing post meta data
$term_meta = get_the_category();
//define global variables
$cat_slug = 'test';
//if array of objects is not empty,
if ( ! empty( $term_meta ) ) {
//for object in array with an index of $x...
for($x = 0; $x <= count($term_meta); $x++){
//set $wp_term_object to the object in the array with an index of $x
$wp_term_object = $term_meta[$x];
//set $slug to the value of key 'slug' in object $x
$slug = $wp_term_object->slug;
//if $slug doesn't equal 'featured...
if($slug != 'featured'){
//set $cat_slug equal to $slug
$cat_slug = $slug;
}
}
}
echo do_shortcode('[display-posts category="'. $cat_slug .', featured" posts_per_page="1"]');