这是我最近编写的一个函数,它也可以完成这项工作并且非常灵活
理念:
您首先需要获取我通过的当前帖子 ID get_queried_object_id()
。帖子 ID 将用于检索:
上面的所有信息都将用于tax_query
withget_posts
以从当前帖子中检索所有共享该术语的帖子。在函数中,默认分类是category
,并且post_type
设置为 any 以获取具有此特定术语的所有帖子
同样,为了使代码更快并保证资源安全,我们只需要获取帖子 ID,因为这就是所有需要的
现在是代码的重要部分。我们现在需要确定以下内容:
最后将返回链接。如果当前帖子是数组中的第一个或最后一个帖子并且没有下一个或上一个帖子,我已经设置了消息。你可以在这里决定你想做什么,对于所有重要的事情,剩下的代码
为了使代码更快、更高效,我使用了Transient API,您可以继续阅读。我还使用transition_post_status
动作挂钩来挂钩一个函数,以便在帖子的帖子状态发生变化时删除这些瞬态。这包括正在发布的新帖子、正在更新的帖子以及已删除/取消删除的帖子
编码:
这是代码。这进入你的functions.php
function get_post_link( $taxonomy = 'category', $post_type = [ 'any' ] ) {
$id = get_queried_object_id(); // Get the current post ID
$transient_id = 'post_number_' . md5( $id . $taxonomy . implode( ',', $post_type ) ); //Create a unique transient id
if ( false === ( $links = get_transient( $transient_id ) ) ) {
// Get the terms a post belongs to
$terms = wp_get_post_terms( $id, $taxonomy, array( 'fields' => 'ids' ) );
// Use a tax_query to get all posts from the given term
// Just retrieve the ids to speed up the query
$post_args = [
'post_type' => $post_type,
'fields' => 'ids',
'posts_per_page' => -1,
'tax_query' => [
[
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $terms[0],
'include_children' => false,
],
],
];
// Get all the posts having the given term from all post types
$q = get_posts( $post_args );
//Get the current post position. Will be used to determine next/previous post
$current_post_position = array_search( $id, $q );
// Get the previous/older post ID
if ( array_key_exists( $current_post_position + 1 , $q ) ) {
$previous = $q[$current_post_position + 1];
}
// Get post title link to the previous post
if( isset( $previous ) ) {
$previous_post = get_post( $previous );
$previous_post_link = get_permalink( $previous );
$previous_title = '<a href="' . $previous_post_link . '">' . $previous_post->post_title . '</a></br>';
}
// Get the next/newer post ID
if ( array_key_exists( $current_post_position - 1 , $q ) ) {
$next = $q[$current_post_position - 1];
}
// Get post title link to the next post
if( isset( $next ) ) {
$next_post = get_post( $next );
$next_post_link = get_permalink( $next );
$next_title = '<a href="' . $next_post_link . '">' . $next_post->post_title . '</a></br>';?><pre><?php var_dump($next_title); ?></pre><?php
}
// The returned post links
if( isset( $previous_title, $next_title ) ) {
$links = [
'previous_post' => $previous_title,
'next_post' => $next_title,
];
}elseif( !isset( $previous_title ) && $next_title ) {
$links = [
'previous_post' => 'You are currently viewing the newest post',
'next_post' => $next_title,
];
}elseif( $previous_title && !isset( $next_title ) ) {
$links = [
'previous_post' => $previous_title,
'next_post' => 'You are currently viewing the last post',
];
}
set_transient( $transient_id, $links, 7 * DAY_IN_SECONDS );
}
return (object)$links;
}
add_action( 'transition_post_status', function ( $new_status, $old_status, $post )
{
global $wpdb;
$wpdb->query( "DELETE FROM $wpdb->options WHERE `option_name` LIKE ('_transient%_post_number_%')" );
$wpdb->query( "DELETE FROM $wpdb->options WHERE `option_name` LIKE ('_transient_timeout%_post_number_%')" );
}, 10, 3 );
如何使用:
您现在可以在 single.php 中使用如下代码。默认分类是category
,帖子类型是any
. 如果您的自定义分类被调用mytax
,您可以使用这样的代码
if( function_exists( 'get_post_link' ) ) {
$post_links = get_post_link( 'mytax' );
echo $post_links->previous_post . '</br>' . $post_links->next_post;
}