【问题标题】:How to Show All Posts from Relationship Field如何显示关系字段中的所有帖子
【发布时间】:2019-08-31 05:18:52
【问题描述】:

我有两种自定义帖子类型:员工和位置。在员工的 CPT 中,我使用 ACF 关系字段选择相应的位置。

在前端,我正在显示一个位置并列出该位置的所有工作人员。然而,我的问题是我无法让所有工作人员都出席。最多只能显示 5 个。

我目前在一个地点分配了 7 名员工。前端仅显示 5 个。如果我删除正在显示的 5 个中的一个,则另一个将取而代之。

'''

                                <?php while ( have_posts() ) : the_post(); ?>

                                            <?php

                                            /*
                                            *  Query posts for a relationship value.
                                            *  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
                                            */

                                            $staff = get_posts(array(
                                                'post_type' => 'people',
                                                'meta_query' => array(
                                                    array(
                                                        'key' => 'location', // name of custom field
                                                        'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
                                                        'compare' => 'LIKE'
                                                    )
                                                )
                                            ));

                                            ?>
                                            <div class="people-grid">
                                            <?php if( $staff ): ?>
                                                <?php foreach( $staff as $doctor ): ?>
                                                    <?php

                                                    $photo = get_field('photo', $doctor->ID);

                                                    ?>
                                                    <div class="single-person">
                                                            <img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>" />
                                                            <h3><?php echo get_the_title( $doctor->ID ); ?></h3>
                                                            <h4><?php echo get_field('position_title', $doctor->ID); ?></h3>

                                                    </div>
                                                <?php endforeach; ?>
                                            </div>
                                            <?php endif; ?>

                                <?php endwhile; // end of the loop. ?>'''

我希望这个页面有 7 个成员,但我只能看到 5 个:https://aptw.nk-creative.com/locations/goshen-ny/

【问题讨论】:

    标签: wordpress advanced-custom-fields custom-post-type


    【解决方案1】:

    它可能是在听默认的每页发帖设置。

    $staff = get_posts(array(
        'post_type' => 'people',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'location', // name of custom field
                'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
                'compare' => 'LIKE'
            )
        )
    ));
    

    尝试将您的查询更改为上述内容,它应该会覆盖您的默认设置以显示查询中提取的所有内容。

    【讨论】:

      猜你喜欢
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 2019-11-09
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多