Support

Account

Home Forums General Issues get previous and next post link in post objects

Solved

get previous and next post link in post objects

  • Hi, I have a course page, assume id 123. It has a post object sub-field with some lesson(post type) under parent repeater. lesson_listing have section_repeater.

    now is it possible that i am on a certain lesson, present in post object sub field post_link can offer previous and next post object/lesson?

    I am able to list all the post objects lessons but not previous or next.

    
    function post_object_navigation_shortcode( $atts ) {
    
    	if ( ! class_exists( 'ACF' ) ) {
    		return;
    	}
    
    	if ( ! ( is_singular( 'lesson' ) ) ) {
    		return;
    	}
    
    	global $post;
    
    	// get the post/page id where post object field is added.
    	$course_id = get_field( 'lesson_of_course' );
    
    	if ( $course_id ) {
    		echo '<h2> course id is ' . $course_id . '</h2>'; // 6197
    	}
    
    	// Get the post objects for the current field
    	$post_objects = get_sub_field( 'post_link', $course_id );
    
    	// Get the current post object index
    	$current_index = array_search( $post->ID, array_column( $post_objects, 'ID' ) );
    
    	// Check if there is a next post object
    	if ( isset( $post_objects[ $current_index + 1 ] ) ) {
    		$next_post_id = $post_objects[ $current_index + 1 ]->ID;
    		$next_post_title = get_the_title( $next_post_id );
    		$next_post_url = get_permalink( $next_post_id );
    		$next_post_link = "<a href='{$next_post_url}'>{$next_post_title}</a>";
    	} else {
    		$next_post_link = 'No next lesson';
    	}
    
    	// Check if there is a previous post object
    	if ( isset( $post_objects[ $current_index - 1 ] ) ) {
    		$prev_post_id = $post_objects[ $current_index - 1 ]->ID;
    		$prev_post_title = get_the_title( $prev_post_id );
    		$prev_post_url = get_permalink( $prev_post_id );
    		$prev_post_link = "<a href='{$prev_post_url}'>{$prev_post_title}</a>";
    	} else {
    		$prev_post_link = 'No previous lesson';
    	}
    
    	// Return the links
    	$output = "<div class='post-object-navigation'>";
    	$output .= "<div class='prev-post'>{$prev_post_link}</div>";
    	$output .= "<div class='next-post'>{$next_post_link}</div>";
    	$output .= "</div>";
    	return $output;
    }
    add_shortcode( 'lesson_navigation', 'post_object_navigation_shortcode' );
    
  • your code does not make much sense to me.

    You are calling get_sub_field() but you do not have a have_rows() loop. You cannot get sub fields of a repeater without a loop.

    From there is just gets more confusing.

    A better explanation of the fields being used here might help.

  • 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' );
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.