function faq_loops($atts) { // Default attributes. $default = array( 'category' => '', ); // Merge user-defined attributes with defaults. $button_attrs = shortcode_atts($default, $atts); // Argument to fetch FAQs based on category. $arg = array( 'post_type' => 'faqs', 'posts_per_page' => -1, 'tax_query' => array( array( 'taxonomy' => 'faqs_category', 'field' => 'slug', 'terms' => $button_attrs['category'], // Use category attribute passed in shortcode // 'operator' => 'IN', ), ), ); // print_r($arg); // Display FAQs $faqsPost = new WP_Query($arg); ?> <div id="mainfaqs"> <?php if ($faqsPost->have_posts()) : ?> <?php while ($faqsPost->have_posts()) : $faqsPost->the_post(); ?> <h2><?php the_title(); ?></h2> <p><?php the_content(); ?></p> <?php endwhile; ?> <?php else : ?> <p>No FAQs found in this category.</p> <?php endif; ?> </div> <?php wp_reset_postdata(); } add_shortcode('mainfaqs', 'faq_loops'); // pass shortcode like this: [mainfaqs category="mainhome"]
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter