Answer the question
In order to leave comments, you need to log in
How to set the current post in WordPress?
WordpPress has a get_next_post() function that returns the next post.
Question: in order to return the next post, WordPress needs to know the current one, how to set this current post itself, knowing the post ID, in order to use the get_next_post () function?
Answer the question
In order to leave comments, you need to log in
The get_next_post() function works in the_post() loop. In order to install the current post on your own, you need to create an additional loop and get the link you are interested in in it, for example:
$myid = 999;
$next_post = '';
$previous_post = '';
$args = array('p'=> $myid, 'post_type' => 'any');
$my_posts = new WP_Query($args);
if($my_posts){
foreach( $my_posts as $post ){
$next_post = get_next_post_link();
$previous_post = get_previous_post_link();
}
}
wp_reset_postdata();
$myid = 999;
$next_post = get_around_post ($myid, 'ASC');
$previous_post = get_around_post ($myid, 'DESC');
function get_around_post ($custom_id, $side){
global $posts;
$category = get_the_category();
rsort( $category );
$cat_add_id = $category[0]->term_id;
$around_post = '';
$my_posts = get_posts( array(
'cat' => $cat_add_id,
'posts_per_page' => 1,
'exclude' => $custom_id,
'order' => $side,
) );
if( $my_posts ){
foreach( $my_posts as $post ){
$around_post = '<a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a>';
}
}
wp_reset_postdata();
return $around_post;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question