questions

How To Implement Questions & Answers Block In WP?

Rate this post

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

  • created custom fields (question, answer) for page template – post
  • inserted to the page where I need “Questions & Answers” block, so it looks like those:

 

<?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:

  • Page “Main” -> Post “Questions & Answers To Main Page” (in Category “Questions & Answers For Main Page”) and here I’m able to add only 1 question and 1 answer, if I need more – I need to create new post. Maybe there is a better solution for this in WordPress?

 

✔️Solution:

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;

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button