post object

Solutions on MaxInterview for post object by the best coders in the world

showing results for - "post object"
Matías
04 Sep 2016
1<?php
2
3// DISPLAY CONTENT FOR SINGLE POST OBJECT
4
5$post_object = get_field('post_object');
6
7if( $post_object ): 
8
9	// override $post
10	$post = $post_object;
11	setup_postdata( $post ); 
12
13	?>
14    <div>
15    	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
16    	<span>Post Object Custom Field: <?php the_field('field_name'); ?></span>
17    </div>
18    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
19<?php endif; ?>
Jacopo
05 Jun 2020
1<?php
2
3// DISPLAY DATA FOR MULTIPLE POST OBJECTS
4
5/*
6*  Loop through post objects (assuming this is a multi-select field) ( setup postdata )
7*  Using this method, you can use all the normal WP functions as the $post object is temporarily initialized within the loop
8*  Read more: http://codex.wordpress.org/Template_Tags/get_posts#Reset_after_Postlists_with_offset
9*/
10
11$post_objects = get_field('post_objects');
12
13if( $post_objects ): ?>
14    <ul>
15    <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
16        <?php setup_postdata($post); ?>
17        <li>
18            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
19            <span>Post Object Custom Field: <?php the_field('field_name'); ?></span>
20        </li>
21    <?php endforeach; ?>
22    </ul>
23    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
24<?php endif;
25
26/*
27*  Loop through post objects (assuming this is a multi-select field) ( don't setup postdata )
28*  Using this method, the $post object is never changed so all functions need a seccond parameter of the post ID in question.
29*/
30
31$post_objects = get_field('post_objects');
32
33if( $post_objects ): ?>
34    <ul>
35    <?php foreach( $post_objects as $post_object): ?>
36        <li>
37            <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a>
38            <span>Post Object Custom Field: <?php the_field('field_name', $post_object->ID); ?></span>
39        </li>
40    <?php endforeach; ?>
41    </ul>
42<?php endif;
43
44?>
Daniel
06 Jun 2017
1<?php
2$featured_post = get_field('featured_post');
3if( $featured_post ): ?>
4    <h3><?php echo esc_html( $featured_post->post_title ); ?></h3>
5<?php endif; ?>
similar questions
queries leading to this page
post object