How to pull multiple ACF post objects from a page and use it in a ul

Question

enter image description here

I have three post objects in my page above, Sub Page 1, Sub Page 2, Sub Page 3, which points to individual custom post type.

I am trying to take each post object and insert in a unordered list like this:

<!-- if ATLEAST one subpage # has a value, display the UL -->
<ul>
   <!-- if subpage 1 has a value -->
   <li>{subpage 1 title}</li>
   <!-- if subpage 2 has a value -->
   <li>{subpage 2 title}</li>
</ul>

This is my code but it only retrieves one, subpage 1 and doesn’t return the rest:

<?php
    $args = array(
        'post_type' => 'page',
        'posts_per_page' => '-1',
        'post_status' => 'publish',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key'     => 'active_page',
                'value'   => 'yes',
                'compare' => '=',
            ),
        ),
    );
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post(); ?>
            <li><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo the_title(); ?></a>
                <?php $post_object01 = get_field('sub_page_1');
                    if( $post_object01 ): ?>
                        <ul>
                    <?php $post = $post_object01;
                    setup_postdata( $post ); ?>
                            <li>
                                <a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                            </li>
                            <?php wp_reset_postdata();  ?>
                            <?php
                                $post_object02 = get_field('sub_page_2');
                                if( $post_object02 ):
                                    $post = $post_object02;
                                    setup_postdata( $post ); ?>
                                    <li>
                                        <a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                                    </li>
                                <?php endif;
                            ?>
                            <?php wp_reset_postdata();  ?>
                        </ul>
                    <?php endif; ?> 
            </li>
        <?php }
    }
?>

I don’t have ACF Pro so I can’t use repeater field unfortunately.

How can I fix my code so I am able to retrieve each sub page # post and get the permalink and title to add it to the UL.

0
, Si8 2 years 2019-12-28T15:28:08-05:00 0 Answers 95 views 0

Leave an answer

Browse
Browse