Getting the next / previous message id using the current message id in Wordpress

I want to write a custom function next / prev to dynamically display message information in a fancybox popup. Therefore, I need to use PHP to get the next and previous message identifier based on what is currently displayed in the message. I know what the current message identifier is, and I can send it to functions normally, but I cannot figure out how to use this identifier to get contiguous identifiers.

Edit: Here is my code so far (which doesn't work)

<?php require_once("../../../wp-blog-header.php"); if (isset($_POST['data'])){ $post_id = $_POST['data']; }else{ $post_id = ""; } $wp_query->is_single = true; $this_post = get_post($post_id); $in_same_cat = false; $excluded_categories = ''; $previous = false; $next_post = get_adjacent_post($in_same_cat,$excluded_categories,$previous); $post_id = $next_post->id; $title = $next_post->post_title; $dataset = array ( "postid"=>$post_id, "posttitle"=>$title ); //Because we want to use json, we have to place things in an array and encode it for json. //This will give us a nice javascript object on the front side. echo json_encode($dataset); ?> 
+4
source share
3 answers

get_adjacent_post() uses the global $post as its reference point, so you will want to replace this:

 $this_post = get_post($post_id); 

with this:

 global $post; $post = get_post($post_id); 

WordPress also provides get_next_post() and get_previous_post() , which you can use here instead of using get_adjacent_post() with all of these arguments. Here is the final product:

 <?php require_once("../../../wp-blog-header.php"); if (isset($_POST['data'])){ $post_id = $_POST['data']; }else{ $post_id = ""; } $wp_query->is_single = true; global $post; $post = get_post($post_id); $previous_post = get_previous_post(); $next_post = get_next_post(); $post_id = $next_post->id; $title = $next_post->post_title; $dataset = array ( "postid"=>$post_id, "posttitle"=>$title ); //Because we want to use json, we have to place things in an array and encode it for json. //This will give us a nice javascript object on the front side. echo json_encode($dataset); ?> 

I'm not sure which keys you would like to use for the identifiers and headers of the previous and next messages in the $dataset , so I will leave this as it is for now.

+6
source

To make this work, I had to change $next_post->id; on $next_post->id; , i.e. capital letters ID .

I used it in Wordpress on index.php, like this - I inserted this at the beginning of the loop:

 <div class="work-post" id="<?php the_ID(); ?>"> 

then in a loop i use this: `

  if (isset($_POST['data'])){ $post_id = $_POST['data']; }else{ $post_id = ""; } $wp_query->is_single = true; global $post; $post = get_post($post_id); $next_post = get_next_post(); $previous_post = get_previous_post(); ?> <!-- call only if a value exists --> <?php if($next_post) : ?> <a href="#<?php echo $next_post->ID;?>" class="anchorLink"><div>PREV.</div></a> <?php endif; ?> <!-- call only if a value exists --> <!-- call only if a value exists --> <?php if($previous_post) : ?> <a href="#<?php echo $previous_post->ID;?>" class="anchorLink"><div>NEXT</div></a> <?php endif; ?> <!-- call only if a value exists -->` 
+3
source

This is my code, it worked well. $ post_id - current message identifier.

 function get_previous_post_id( $post_id ) { // Get a global post reference since get_adjacent_post() references it global $post; // Store the existing post object for later so we don't lose it $oldGlobal = $post; // Get the post object for the specified post and place it in the global variable $post = get_post( $post_id ); // Get the post object for the previous post $previous_post = get_previous_post(); // Reset our global object $post = $oldGlobal; if ( '' == $previous_post ) return 0; return $previous_post->ID; } function get_next_post_id( $post_id ) { // Get a global post reference since get_adjacent_post() references it global $post; // Store the existing post object for later so we don't lose it $oldGlobal = $post; // Get the post object for the specified post and place it in the global variable $post = get_post( $post_id ); // Get the post object for the next post $next_post = get_next_post(); // Reset our global object $post = $oldGlobal; if ( '' == $next_post ) return 0; return $next_post->ID; } 

Then you just call the function. Example:

 echo get_previous_post_id( $current_id ); echo get_next_post_id( $current_id ); 

Good luck

+1
source

All Articles