如何在 WordPress 中实现问答块

How To Implement Questions & Answers Block In WordPress

我的网站布局中有“问题与解答”块(它由“h3”- 问题和“p”- 答案组成)。我找到了这样的实现:

<?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();?>

但问题是我需要为 post(和 post 本身)为我需要“问题与答案”块的每个页面创建一个新类别,例如:

你用ACF插件吗?

在你的页面问答 创建一个包含 2 个字段 QuestionAnswer

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

如果您使用的是 YOAST SEO 插件,则有一个 gutemberg 块可以满足您的需求。 https://yoast.com/how-to-build-an-faq-page/

PHP 此 ACF 字段的代码

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;