【问题标题】:How to display posts with plugin (advanced custom fields) field groups?如何显示带有插件(高级自定义字段)字段组的帖子?
【发布时间】:2021-02-11 10:27:33
【问题描述】:

我有一个 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 hello-world

的帖子相关联的特色图片

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

(c) 行视图和列视图都是开关,可以启用是或没有。默认情况下,它设置为 No。

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

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

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

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

我想要实现的是显示那些将行视图切换为是的帖子。

问题陈述:

我想知道我需要在上面的 php/wordpress 代码中进行哪些更改,以便它只显示那些将 Row View 开关启用为 Yes 的帖子。

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

【问题讨论】:

  • 您只需要使用元字段来白化自定义查询。然后你只会在循环中获取你想要的 - 文档在这里:advancedcustomfields.com/resources/query-posts-custom-fields/…
  • @Stender 我想知道你能否给我指点如何实现这一目标。我已经在赏金问题上标记了我的问题。如果您需要任何信息,请告诉我。

标签: wordpress advanced-custom-fields posts


【解决方案1】:

快速/草率的解决方案:

<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

【讨论】:

  • 我尝试了您的快速/草率解决方案。它没有用。在对代码进行一个小的改动后,它就开始工作了。检查小提琴3v4l.org/4dGjg
  • 与将 != 更改为 == 的概念相同: if(get_field('in_row_view' ) == '1') { continue; }
猜你喜欢
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
  • 2017-01-08
  • 1970-01-01
  • 2012-11-26
  • 2013-10-16
相关资源
最近更新 更多