Post type get short code with category slug (working 100%)
Fri May 05 2023 13:41:02 GMT+0000 (Coordinated Universal Time)
Saved by
@hamzahanif192
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"]
content_copyCOPY
Comments