Yes, i already revised the code and this works:
function post_object_navigation_shortcode() {
global $post;
// get the post/page id where post object field is added.
$course_id = get_field( 'lesson_of_course' );
// get current post id, this can be also a post object.
$current_post_link_id = get_the_ID();
// get all the post_link sub field values.
$post_objects = array();
if ( have_rows( 'lesson_listing', $course_id ) ) :
while ( have_rows( 'lesson_listing', $course_id ) ) :
the_row();
// check for rows (sub repeater)
if ( have_rows( 'section_repeater', $course_id ) ) :
while ( have_rows( 'section_repeater', $course_id ) ) :
the_row();
$my_field = get_sub_field( 'post_link' );
if ( ! empty( $my_field ) ) {
$post_objects[] = $my_field;
}
endwhile;
endif;
endwhile;
endif;
// get the current index of the current post object
$current_index = array_search( $current_post_link_id, array_column( $post_objects, 'ID' ) );
// output the previous post object title and link
if ( $current_index > 0 ) {
$previous_post = $post_objects[ $current_index - 1 ];
$post = get_post( $previous_post );
setup_postdata( $post );
$title = get_the_title();
$link = get_the_permalink();
echo '<a href="' . esc_url( $link ) . '">' . esc_html( $title ) . '</a>';
}
// output the next post object title and link
if ( $current_index < count( $post_objects ) - 1 ) {
$next_post = $post_objects[ $current_index + 1 ];
$post = get_post( $next_post );
setup_postdata( $post );
$title = get_the_title();
$link = get_the_permalink();
echo '<a href="' . esc_url( $link ) . '">' . esc_html( $title ) . '</a>';
}
wp_reset_postdata();
}
add_shortcode( 'lesson_navigation', 'post_object_navigation_shortcode' );
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');
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');
Here is the current setup in screenshot:
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.