I am trying to create a shortcode that displays my post type: advanced_topic but want to add the category parameter such as
[advanced-topics category="parole"]
to display only topics under that category.
This is the code I have so far but I feel like I'm missing something as it's not working... Any help is appreciated. Thank you.
function display_advanced_topics(){
extract( shortcode_atts( array( 'category_name' => 'criminal-records' ), $args ) ); // $categoryName variable will be initialized to 'criminal-records' if the shortcode is missing the 'category' parameter
$string = '';
$args = array(
'post_type' => 'advanced_topic',
'posts_per_page' => -1,
'post_status' => 'publish',
'category_name' => $categoryName
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
$string .= '<div class="advanced-topics">';
while( $query->have_posts() ){
$query->the_post();
$string .=
'<div class="row advanced-topic">'
.'<div class="content">
<div class="title"><?php get_the_title(); ?></div>
</div>'
.'</div>';
}
$string .= '</div>';
}
wp_reset_postdata();
return $string;
}
add_shortcode( 'advanced-topics', 'display_advanced_topics' );