Completed FAQ postype with category slug
Fri Sep 13 2024 06:08:21 GMT+0000 (Coordinated Universal Time)
Saved by @hamzahanif192
function create_faqs() { $labels = array( 'name' => _x('FAQs', 'Post Type General Name', 'textdomain'), 'singular_name' => _x('FAQ', 'Post Type Singular Name', 'textdomain'), 'menu_name' => _x('FAQs', 'Admin Menu text', 'textdomain'), 'name_admin_bar' => _x('FAQs', 'Add New on Toolbar', 'textdomain'), 'archives' => __('FAQs Archives', 'textdomain'), 'attributes' => __('FAQs Attributes', 'textdomain'), 'parent_item_colon' => __('Parent FAQs:', 'textdomain'), 'all_items' => __('All FAQs', 'textdomain'), 'add_new_item' => __('Add New FAQs', 'textdomain'), 'add_new' => __('Add New', 'textdomain'), 'new_item' => __('New FAQs', 'textdomain'), 'edit_item' => __('Edit FAQs', 'textdomain'), 'update_item' => __('Update FAQs', 'textdomain'), 'view_item' => __('View FAQs', 'textdomain'), 'view_items' => __('View FAQs', 'textdomain'), 'search_items' => __('Search FAQs', 'textdomain'), 'not_found' => __('Not found', 'textdomain'), 'not_found_in_trash' => __('Not found in Trash', 'textdomain'), 'featured_image' => __('Featured Image', 'textdomain'), 'set_featured_image' => __('Set featured image', 'textdomain'), 'remove_featured_image' => __('Remove featured image', 'textdomain'), 'use_featured_image' => __('Use as featured image', 'textdomain'), 'insert_into_item' => __('Insert into FAQs', 'textdomain'), 'uploaded_to_this_item' => __('Uploaded to this FAQs', 'textdomain'), 'items_list' => __('FAQs list', 'textdomain'), 'items_list_navigation' => __('FAQs list navigation', 'textdomain'), 'filter_items_list' => __('Filter FAQs list', 'textdomain'), ); $rewrite = array( 'slug' => 'faqS', 'with_front' => true, 'pages' => true, 'feeds' => true, ); $args = array( 'label' => __('FAQs', 'textdomain'), 'description' => __('', 'textdomain'), 'labels' => $labels, 'menu_icon' => 'dashicons-format-status', 'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'page-attributes', 'post-formats', 'custom-fields'), 'taxonomies' => array(), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => true, 'hierarchical' => true, 'exclude_from_search' => true, 'show_in_rest' => true, 'publicly_queryable' => true, 'capability_type' => 'post', 'rewrite' => $rewrite, ); register_post_type('faqs', $args); register_taxonomy('faqs_category', 'faqs', array('hierarchical' => true, 'label' => 'Category', 'query_var' => true, 'rewrite' => array('slug' => 'faqs-category'))); } add_action('init', 'create_faqs', 0); 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" class="faq-list"> <?php if ($faqsPost->have_posts()) : ?> <?php while ($faqsPost->have_posts()) : $faqsPost->the_post(); ?> <div class="faq"> <div class="faq__question"> <div class="faq__ttl "><?php the_title(); ?></div> <div class="faq__close"></div> </div> <div class="faq__answer" style="display: none; transition: max-height 0.3s ease;"> <div class="faq__txt" style=" "> <?php the_content(); ?> </div> </div> </div> <?php $x++; ?> <?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"] //jquery jQuery(function () { jQuery(".faq__question").on('click', function () { let box = jQuery(this).closest(".faq"); let openBox = jQuery(".faq_active"); // Check if the clicked FAQ is not the currently open one if (box.hasClass('faq_active')) { // If it's the same one, just toggle it box.find('.faq__answer').slideUp(300); box.removeClass('faq_active'); } else { // Otherwise, close the currently open FAQ and open the new one openBox.find('.faq__answer').slideUp(300); openBox.removeClass('faq_active'); box.find('.faq__answer').slideDown(300); box.addClass('faq_active'); } }); });
Comments