php 在 Wordpress 中使用当前帖子 ID 获取下一个/上一个帖子 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6324421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Next/Previous Post ID using Current Post ID in Wordpress
提问by Jarrod Ballou
I want to write a custom next/prev function to dynamically display post information in a fancybox pop-up. So I need to use PHP to get the next and previous Post ID based on whatever Post is currently showing. I know what the current post ID is and I can send that to a function ok, but I can't figure out how to use that ID to obtain the adjacent IDs.
我想编写一个自定义的 next/prev 函数,在fancybox 弹出窗口中动态显示帖子信息。所以我需要使用 PHP 根据当前显示的任何帖子获取下一个和上一个帖子 ID。我知道当前的帖子 ID 是什么,我可以将它发送到一个函数,但我不知道如何使用该 ID 来获取相邻的 ID。
Edit:Here is my code so far (which is not working)
编辑:到目前为止,这是我的代码(不起作用)
<?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);
?>
回答by Tom
get_adjacent_post()
uses the global $post
as its reference point, so you'll want to replace this:
get_adjacent_post()
使用全局$post
作为其参考点,因此您需要替换它:
$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 those arguments. Here's the final product:
WordPress 还提供了get_next_post()
and 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);
?>
I'm not sure what keys you'd like to use for the IDs and titles of the previous and next posts in the $dataset
array, so I'll leave that as is for now.
我不确定您想对$dataset
数组中上一篇和下一篇文章的 ID 和标题使用什么键,所以我暂时保留它。
回答by Arvid Eriksson
To get this to work I had to change $next_post->id;
to $next_post->ID;
, i.e. capitalise ID
.
为了让它工作,我不得不$next_post->id;
改为$next_post->ID;
,即大写ID
.
I used it in Wordpress on index.php like this – I inserted this at the top of the loop:
我在 index.php 上的 Wordpress 中使用它,就像这样 - 我将它插入到循环的顶部:
<div class="work-post" id="<?php the_ID(); ?>">
then within the 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 -->`
回答by Calvin NGUYEN
This is my code, it worked good. $post_id is current post ID.
这是我的代码,效果很好。$post_id 是当前帖子 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;
}
Then you just call function. Example:
然后你只需调用函数。例子:
echo get_previous_post_id( $current_id );
echo get_next_post_id( $current_id );
Good luck!
祝你好运!