userName
userName

Reputation: 955

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>

Upvotes: 0

Views: 257

Answers (1)

Chris Haas
Chris Haas

Reputation: 55447

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

Upvotes: 1

Related Questions