Php 在Wordpress中使用当前帖子ID获取下一个/上一个帖子ID
我想编写一个自定义的next/prev函数,以便在fancybox弹出窗口中动态显示帖子信息。因此,我需要使用PHP根据当前显示的帖子获取下一个和上一个帖子ID。我知道当前post ID是什么,我可以将其发送到函数ok,但我不知道如何使用该ID获取相邻ID 编辑:这是到目前为止我的代码(不起作用)Php 在Wordpress中使用当前帖子ID获取下一个/上一个帖子ID,php,jquery,ajax,wordpress,fancybox,Php,Jquery,Ajax,Wordpress,Fancybox,我想编写一个自定义的next/prev函数,以便在fancybox弹出窗口中动态显示帖子信息。因此,我需要使用PHP根据当前显示的帖子获取下一个和上一个帖子ID。我知道当前post ID是什么,我可以将其发送到函数ok,但我不知道如何使用该ID获取相邻ID 编辑:这是到目前为止我的代码(不起作用) 获取相邻的帖子()使用全局$post作为其参考点,因此您需要替换此: $this_post = get_post($post_id); 为此: global $post; $post = get_
获取相邻的帖子()
使用全局$post
作为其参考点,因此您需要替换此:
$this_post = get_post($post_id);
为此:
global $post;
$post = get_post($post_id);
WordPress还提供了get\u next\u post()
和get\u previous\u post()
,您可以在此处使用这些参数,而不是在所有这些参数中使用get\u nexted\u 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);
?>
我不确定您想在
$dataset
数组中使用哪些键来显示上一篇和下一篇文章的id和标题,所以我现在就不做了。要让它工作,我必须更改$next\u post->id代码>到$next\u post->ID代码>,即大写ID
我在index.php上的Wordpress中使用了它,如下所示–我在循环的顶部插入了这个:
<div class="work-post" id="<?php the_ID(); ?>">
这是我的代码,效果很好$post_id是当前的post id
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;
}
然后调用函数。例如:
echo get_previous_post_id( $current_id );
echo get_next_post_id( $current_id );
祝你好运
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;
}
echo get_previous_post_id( $current_id );
echo get_next_post_id( $current_id );