为什么我只收到一篇帖子,而不是所有 ID 的帖子

问题描述 投票:0回答:1

我尝试在 WordPress 的页面上按 ID 显示帖子,帖子 ID 是从元框值获取的,我尝试使用以下代码:

<div class="grve-row grve-bookmark">
    <?php
    // Get the Instagram Post IDs from the custom metabox
    $instagram_post_ids = get_post_meta(get_the_ID(), 'instagram_post_id', false);

    // Check if there are Instagram Post IDs
    if ($instagram_post_ids) :
        $args = array(
            'post_type' => 'post', // Change to your post type if needed
            'post__in' => $instagram_post_ids,
            'orderby' => 'post__in',
        );

        $custom_query = new WP_Query($args);

        if ($custom_query->have_posts()) :
            while ($custom_query->have_posts()) : $custom_query->the_post();
    ?>
                <div class="wpb_column grve-column grve-bookmark grve-column-1-3">
                    <?php if (has_post_thumbnail()) : ?>
                        <div class="featured-image">
                            <a href="<?php the_permalink(); ?>">
                                <?php the_post_thumbnail(300, true); // Adjust image size as needed ?>
                            </a>
                        </div>
                    <?php endif; ?>
                    <h4 class="post-title">
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </h4>
                </div>
    <?php
            endwhile;
        else :
            // No posts found
        endif;

        // Reset post data
        wp_reset_postdata();
    else :
        // No Instagram Post IDs found
    endif;
    ?>
</div>

使用元框值 [2204, 2218, 2187] 的代码,我仅获得显示第一个 id 的帖子,在本例中,仅显示 id 为 2204 的帖子

如何显示所有帖子而不是仅显示第一个帖子?

php wordpress custom-wordpress-pages
1个回答
0
投票

我相信您需要指定

posts_per_page
查询参数。

例如,从查询中获取 5 个帖子:

$args = array(
    'post_type' => 'post', // Change to your post type if needed
    'post__in' => $instagram_post_ids,
    'orderby' => 'post__in',
    'posts_per_page' => 5,
);

或者您可以使用值

-1
返回所有帖子,但当您有大量帖子时,请注意潜在的性能问题。

$args = array(
        'post_type' => 'post', // Change to your post type if needed
        'post__in' => $instagram_post_ids,
        'orderby' => 'post__in',
        'posts_per_page' => -1,
    );
© www.soinside.com 2019 - 2024. All rights reserved.