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

问题描述

我有一个 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)所有这些图像都是与 post_type hello-world

的帖子相关的wordpress中的特色图像。

(b)每个帖子都有两个字段,行视图(in_row_view)列视图(in_column_view)来自“高级自定义字段插件输入。我不确定是否可以将这两个字段称为自定义字段

(c) 行视图列视图都是开关,可以将其启用为或否。认情况下,它设置为

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

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

对于列视图,我正在使用:

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

我想要实现的是显示那些将行视图开关启用为的帖子。

问题陈述:

我想知道我需要在上面的PHP / wordpress代码中进行哪些更改,以便仅显示行视图开关启用为的那些帖子。 / p>

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

解决方法

快速/草率解决方案:

<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