问题描述
|
我想编写一个自定义的next / prev函数,以在fancybox弹出窗口中动态显示帖子信息。因此,我需要使用PHP根据当前显示的帖子获取下一个和上一个帖子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);
?>
解决方法
get_adjacent_post()
使用全局$post
作为其参考点,因此您将需要替换它:
$this_post = get_post($post_id);
有了这个:
global $post;
$post = get_post($post_id);
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);
?>
我不确定您要在$dataset
数组中的上一个和下一个帖子的ID和标题中使用哪些键,所以我现在将其保留。
, 为了使它起作用,我必须将ѭ10更改为$next_post->ID;
,即将ID
大写。
我像这样在index.php的Wordpress中使用了它–我在循环的顶部插入了它:
<div class=\"work-post\" id=\"<?php the_ID(); ?>\">
然后在循环中,我用这个:
`
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 -->`
, 这是我的代码,效果很好。 $ 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;
}
然后,您只需调用函数。例:
echo get_previous_post_id( $current_id );
echo get_next_post_id( $current_id );
祝好运!