Home › Forums › General Issues › Multiple Post Object in Group
Hi, I have created a post selector group post_grid
with 5 post objects each will output single post. Post object fields are:
ep_review
ranking
news
games
manga
However, the following code not seems to work and output the current page.
// Shortcode [post_grid]
if ( ! function_exists('post_grid_shortcode') ) {
function post_grid_shortcode( $atts ){
global $post;
if( have_rows('post_grid') ):
while( have_rows('post_grid') ): the_row();
$featured_posts = get_sub_field('news');
if( $featured_posts ):
// foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post->ID );
$title = get_the_title( $featured_post->ID );
?>
<a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
<?php
//endforeach;
endif;
endwhile;
endif;
}
}
add_shortcode('post_grid', 'post_grid_shortcode');
I am taking help from https://www.advancedcustomfields.com/resources/group/ to create the group and https://www.advancedcustomfields.com/resources/post-object/ to output post object with get_sub_field()
I have tried both setup_postdata and without setup_postdata code for post object. ANy help would be really appreciated.
Here is the current setup in screenshot:
This code seems to work but output sample-page and few blank HTML. It seems like i am missing something simple.
// Shortcode [post_grid]
if ( ! function_exists('post_grid_shortcode') ) {
function post_grid_shortcode( $atts ){
global $post;
if( have_rows('post_grid') ):
while( have_rows('post_grid') ): the_row();
// 2
$featured_posts = get_sub_field('ranking');
if( $featured_posts ):
//
foreach( $featured_posts as $post ):
setup_postdata($post);
//foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post->ID );
$title = get_the_title( $featured_post->ID );
?>
<a href="<?php echo esc_url( $permalink ); ?>" class="10"><?php echo esc_html( $title ); ?></a>
<a href="<?php the_permalink(); ?>" class="11"><?php the_title(); ?></a>
<?php
//endforeach;
endforeach;
wp_reset_postdata();
//
endif;
//2
endwhile;
endif;
}
}
add_shortcode('post_grid', 'post_grid_shortcode');
Is you post object field set to allow multiple selections?
If not then the field will return a single post object, not an array of posts, so you do not need to loop over what is returned.
Hi, it not allow multiple selections, single selection for each post object (shared in 2nd reply).
Here what i have tried, seems to work now:
// Shortcode [post_grid]
if ( ! function_exists('post_grid_shortcode') ) {
function post_grid_shortcode( $atts ){
global $post;
if( have_rows('post_grid') ):
while( have_rows('post_grid') ): the_row();
// 2
$featured_posts = get_sub_field('ranking');
if( $featured_posts ):
$permalinkx = get_permalink( $featured_posts->ID );
$titlex = get_the_title( $featured_posts->ID );
$permalink = get_the_permalink();
$title = get_the_title();
echo '<a href="'.$permalink.'" class="11">'.$title.'</a>';
echo '<a href="'.$permalinkx.'" class="11">'.$titlex.'</a>';
endif;
//2
endwhile;
endif;
}
}
add_shortcode('post_grid', 'post_grid_shortcode');
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.