Search code examples
phpwordpressadvanced-custom-fields

How to display Post Object in template?


I have a Post Object field in which a list can be selected and I need to display all the selected titles on the page. Now, with my code, nothing is displayed on the page. How can I solve this?

<ul class="speaker-sessions_list">
<?php
$speaker_sessions = get_field('speaker_sessions');
if( $speaker_sessions ): ?>
<li><?php echo $speaker_sessions->post_title; ?><li>
<?php endif; ?>
</ul>


Solution

  • As noted from the comments, you have an array so you’ll need to iterate over them:

    <?php if( $speaker_sessions = get_field('speaker_sessions') ): ?>
    <ul class="speaker-sessions_list">
        <?php foreach( $speaker_sessions as $speaker_session ): ?>
            <li><?php esc_html_e($speaker_session->post_title); ?><li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>