最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

I have a php/wordpress code (present inside abc.php file) as shown below which displays images of particular size.

php/wordpress:

<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>

The code above displays around 30-40 images from top to bottom with 4 images in one row.

(a) All of those images are featured image in wordpress associated with a post.

(b) Every post has two fields, Row View (in_row_view) and Column View (in_column_view) coming from Advanced Custom Fields plug in. I am not sure if we can call those two fields as custom fields.

(c) Row View and Column View both are switch which can be enabled to Yes or No. By default it is set to No.

(d) For Row View, I am using:

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

For Column View, I am using:

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

What I want to achieve is I want to display those posts which have Row View switch enabled to Yes.

Problem Statement:

I am wondering what changes I need to make in the php/wordpress code above so that it displays only those posts which have Row View switch enabled to Yes.

Probably I need to integrate this code if(get_field('in_row_view' ) == '1'){ } in the php/wordpress code above.

I have a php/wordpress code (present inside abc.php file) as shown below which displays images of particular size.

php/wordpress:

<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>

The code above displays around 30-40 images from top to bottom with 4 images in one row.

(a) All of those images are featured image in wordpress associated with a post.

(b) Every post has two fields, Row View (in_row_view) and Column View (in_column_view) coming from Advanced Custom Fields plug in. I am not sure if we can call those two fields as custom fields.

(c) Row View and Column View both are switch which can be enabled to Yes or No. By default it is set to No.

(d) For Row View, I am using:

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

For Column View, I am using:

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

What I want to achieve is I want to display those posts which have Row View switch enabled to Yes.

Problem Statement:

I am wondering what changes I need to make in the php/wordpress code above so that it displays only those posts which have Row View switch enabled to Yes.

Probably I need to integrate this code if(get_field('in_row_view' ) == '1'){ } in the php/wordpress code above.

Share Improve this question edited Oct 30, 2020 at 20:17 user5447339 asked Oct 29, 2020 at 0:30 user5447339user5447339 819 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

What I would suggest is to create a custom WP_Query. What you would do is something like this (modified from ACF documentation):

<?php

    $posts = get_posts( array(
        'meta_query' => array(
            array(
                'key'   => 'in_row_view',
                'value' => '1',
            )
        )
    ) );

    if( $posts ) {
        foreach( $posts as $post ) {
            // Do something.
       }
    }

So, in your code, you will not use the base while(have_posts()) loop but a loop iterating the post (the foreach in my example).

发布评论

评论列表(0)

  1. 暂无评论