使用Wordpress中的当前帖子ID获取下一个/上一个帖子ID

Jar*_*lou 5 php ajax wordpress jquery fancybox

我想编写一个自定义的next/prev函数来动态显示fancybox弹出窗口中的帖子信息.因此,我需要使用PHP来根据Post当前显示的内容获取下一个和之前的帖子ID.我知道当前的帖子ID是什么,我可以将其发送到函数ok,但我无法弄清楚如何使用该ID来获取相邻的ID.

编辑:这是我的代码到目前为止(这是不行的)

<?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);


?>
Run Code Online (Sandbox Code Playgroud)

Tom*_*Tom 7

get_adjacent_post()使用全局$post作为参考点,因此您需要替换它:

$this_post = get_post($post_id);
Run Code Online (Sandbox Code Playgroud)

有了这个:

global $post;
$post = get_post($post_id);
Run Code Online (Sandbox Code Playgroud)

WordPress的也提供了get_next_post()get_previous_post(),你可以用它来代替使用这里get_adjacent_post()与所有的这些参数.这是最终产品:

<?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);

?>
Run Code Online (Sandbox Code Playgroud)

我不确定你想用什么键来获得$dataset数组中前一个和下一个帖子的ID和标题,所以我现在就把它留下来.