如何显示带有插件(高级自定义字段)字段组的帖子?

How to display posts with plugin (advanced custom fields) field groups?

我有一个 php/wordpress 代码(存在于 abc.php 文件 中),如下所示,它显示图像特定尺寸。

php/wordpress(在abc.php文件内):

<ul id="list-thumbs" class="list-grid">
    <?php
    while ( have_posts() ) : the_post();
        ?>
        <li class="list-grid__thumb shows-grid__item">
                    <a class="list-grid__img-link" href="<?php echo esc_url( get_the_permalink() ); ?>" tabindex="0">
                        <?php if ( has_post_thumbnail() ) { ?>
                            <?php
                            $image_id = get_post_thumbnail_id( get_the_ID() );
                            WORLD\Images\the_img_fit_figure( $image_id, 'list-grid__image', '(min-width: 450px) 50vw, 70vw', false ); ?>
                        <?php } ?>
                    </a>

        </li>
    <?php endwhile; ?>
</ul>

上面的代码从上到下显示大约 30-40 张图片,一行 4 张图片。

(a) 所有这些图像都是 wordpress 中的特色图像,与 post_type 的 post hello-world

(b) 每个 post 都有两个字段,Row View (in_row_view)Column View (in_column_view) 来自 Advanced Custom Fields 插件。我不确定我们是否可以将这两个字段称为自定义字段。

(c) 行视图列视图都是可以开启的开关。默认设置为 No.

(d) 对于行视图,我使用:

if(get_field('in_row_view' ) == '1'){   }

对于列视图,我使用的是:

if( get_field('in_column_view' ) == '1'){   }

我想要实现的是我想显示那些启用了 Row View 开关的 post 到 Yes .

问题陈述:

我想知道我需要对上面的 php/wordpress 代码进行哪些更改,以便它只显示那些具有 Row View 开关的 posts启用

可能我需要在上面的 php/wordpress 代码中集成此代码 if(get_field('in_row_view' ) == '1'){ }

Quick/Sloppy 解法:

<ul id="list-thumbs" class="list-grid">
    <?php
    while ( have_posts() ) : the_post();
        // This will skip the loop
        if(get_field('in_row_view' ) != '1') { continue; }
        ?>
        <li class="list-grid__thumb shows-grid__item">
                    <a class="list-grid__img-link" href="<?php echo esc_url( get_the_permalink() ); ?>" tabindex="0">
                        <?php if ( has_post_thumbnail() ) { ?>
                            <?php
                            $image_id = get_post_thumbnail_id( get_the_ID() );
                            WORLD\Images\the_img_fit_figure( $image_id, 'list-grid__image', '(min-width: 450px) 50vw, 70vw', false ); ?>
                        <?php } ?>
                    </a>

        </li>
    <?php endwhile; ?>
</ul>

...“快速解决方案”的问题在于它不会呈现所需的“每页帖子数”。更简单的方法是使用 meta_query 在查询级别包含或排除...这可以通过使用 pre_get_posts 挂钩“正在运行”的查询来完成。

更优雅的解决方案

function pw_filter_query( $query ) {

    // Your conditions for NOT applying the meta query ... use wp conditionals! :)
    if( !$query->get('post_type') != 'your_post_type ) { return $query; }

        $query->set('meta_key', 'ecpt_color');
        $query->set('meta_value', 'green');

    }

}
add_action('pre_get_posts', 'pw_filter_query', 9999);

Source Reference