How To Implement Questions & Answers Block In WordPress

I have "Questions & Answers" block (it consist of "h3" - question, "p" - answer) in layout of my site. I found an implementation like that:

<?php $posts = get_posts( array() );
        foreach( $posts as $post ){
        setup_postdata($post);?>
        <div class="table_bg">
        <h3 class="question"><?php the_field('question'); ?></h3>
        <p class="answer"><?php the_field('answer'); ?></p></div>
        <?php}
        wp_reset_postdata();?>

but the problem is that I need to create a new category for post (and post itself) for each page where I need "Questions & Answers" block, example:

Upvotes: 0

Views: 136

Answers (1)

Sco
Sco

Reputation: 759

Do you use the ACF plugin?

In your page Q&A Create a repeater field with 2 fields Question and Answer

<?php
$questions = get_field('questions');
if(!empty($questions)){
    foreach ($questions as $key => $question) {
        echo '<h3>'.$question['question'].'</h3>';
        echo '<p>'.$question['answer'].'</p>';
    }
}
?>

If you are using YOAST SEO Plugin, there is a gutemberg block that does what you are looking for. https://yoast.com/how-to-build-an-faq-page/

PHP Code for this ACF Fields

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
    'key' => 'group_602549386ffbd',
    'title' => 'questions & answers',
    'fields' => array(
        array(
            'key' => 'field_60254947fa15d',
            'label' => 'questions',
            'name' => 'questions',
            'type' => 'repeater',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'collapsed' => '',
            'min' => 0,
            'max' => 0,
            'layout' => 'table',
            'button_label' => '',
            'sub_fields' => array(
                array(
                    'key' => 'field_6025494cfa15e',
                    'label' => 'question',
                    'name' => 'question',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'maxlength' => '',
                ),
                array(
                    'key' => 'field_60254952fa15f',
                    'label' => 'answer',
                    'name' => 'answer',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array(
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'maxlength' => '',
                ),
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'page',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
));

endif;

Upvotes: 0

Related Questions