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

php - List all ACF field values across every post on one page

programmeradmin3浏览0评论

I used an ACF repeater field for data sheets/instruction manual download links of products in woocommerce. There's 50 products and each has 1 to 4 download links on their product page. I've been searching for a way to list all the links form all 50 products on one page the way you would query all posts on a page in a list. I don't even know where to start looking and I didn't see anything specific on the ACF forums.

I used an ACF repeater field for data sheets/instruction manual download links of products in woocommerce. There's 50 products and each has 1 to 4 download links on their product page. I've been searching for a way to list all the links form all 50 products on one page the way you would query all posts on a page in a list. I don't even know where to start looking and I didn't see anything specific on the ACF forums.

Share Improve this question asked Jun 12, 2019 at 22:20 Joe BarrettJoe Barrett 236 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You want to start here and here.

Essentially you're just looping through all your posts like normal and then adding some custom fields into it.

Be sure to reference the Repeater Field documentation.

This should work inside a loop. Notice you'll need your post ID.

<ul>
    <?php

    if( have_rows('YOURREPEATERFIELDNAMEHERE', 'POSTIDHERE') ):

        while ( have_rows('YOURREPEATERFIELDNAMEHERE', 'POSTIDHERE') ) : the_row(); ?>


            <li><?php echo get_sub_field('SUBFIELDNAMEHERE'); ?></li>

       <?php endwhile;

    else :

        // no rows found

    endif;

    ?>


    <?php endwhile; ?>
</ul>

I used this.

<?php 

$posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'         => 'product',
    //'orderby'         => 'title',
    //'order'           => 'DESC',
    'tax_query' => array( array(
        'taxonomy'         => 'product_cat',
        'field'            => 'slug', // Or 'term_id' or 'name'
        'terms'            =>  'winter-products' // A slug term
        // 'include_children' => false // or true (optional)
    )),
));

if( $posts ): ?>
    <h3>Winter Products</h3>
    <ul>

    <?php foreach( $posts as $post ): 

        setup_postdata( $post );
        $post_id = get_the_ID();

    ?>


            <?php if( have_rows('extra_info', $post_id) ): ?> 
                <li>

            <strong><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></strong> 

                <ul>

                <?php while( have_rows('extra_info', $post_id) ): the_row(); ?>

                    <li><a href="<?php the_sub_field('link_url'); ?>" target="_blank"><?php the_sub_field('link_title'); ?></a></li>

                <?php endwhile; ?>

                </ul>

            <?php endif; ?>
        </li>

    <?php endforeach; ?>

    </ul>

    <?php wp_reset_postdata(); ?>

<?php endif; ?>
发布评论

评论列表(0)

  1. 暂无评论