我有一个自定义的post类型,并使用get\\u next\\u post()和get\\u previous\\u post()创建一个简单的分页。但是,我需要创建分页循环(如果这是正确的术语)。如果我在第一篇文章中,我需要浏览回最后一篇文章,如果我在最后一篇文章中,我需要浏览到第一篇文章。有没有一个简单的方法可以做到这一点,我还没有看到,但谷歌、这个网站和食品法典并没有很好的帮助:/
我目前的代码是。我曾尝试使用wp\\u list\\u页面和其他功能,但运气不佳。
$next_post = get_next_post();
$prev_post = get_previous_post();
$nav = array(
"next_post" => array(
"url" => get_permalink($next_post->ID),
"id" => $next_post->ID,
"titill" => get_field("vinstri_titill", $next_post->ID)." - ".get_field("vinstri_undirtitill", $next_post->ID)
),
"prev_post" => array(
"url" => get_permalink($prev_post->ID),
"id" => $prev_post->ID,
"titill" => get_field("vinstri_titill", $prev_post->ID)." - ".get_field("vinstri_undirtitill", $prev_post->ID)
),
);
最合适的回答,由SO网友:birgire 整理而成
以下是上一个帖子圈的一个想法:
$next_post = get_next_post();
$prev_post = get_previous_post_circle();
我们定义了此函数:
function get_previous_post_circle($in_same_cat = false, $excluded_categories = \'\'){
$prev_post = get_previous_post($in_same_cat,$excluded_categories);
if($prev_post){
return $prev_post;
}else{
add_filter(\'get_previous_post_where\',\'custom_get_previous_post_where\');
$prev_post = get_previous_post($in_same_cat,$excluded_categories);
remove_filter(\'get_previous_post_where\',\'custom_get_previous_post_where\');
if($prev_post){
return $prev_post;
}else{
return \'\';
}
}
}
以及用于删除特定日期比较的筛选函数:
function custom_get_previous_post_where($where){
$where=" WHERE ".implode(" AND ",array_slice(explode("AND",$where),1));
return $where;
}
目的是模仿
get_previous_post()
具有相同输入参数的函数,但您当然可以使用
get_post()
而是在代码中创建闭环。
SO网友:Francesco Cortese
我的解决方案如果next不为空,请给我循环,但只给第一个permalink:
<?php
$next_post = get_previous_post();
if (!empty( $next_post )): ?>
<a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>"><?php echo esc_attr( $next_post->post_title ); ?></a>
<?php else: ?>
<?php $args = array(
\'post_type\'=> \'post\',
\'orderby\' => \'ID\',
\'post_status\' => \'publish\',
\'order\' => \'ASC\',
\'posts_per_page\' => 1 // this will retrive all the post that is published
);
$result = new WP_Query( $args );
if ( $result-> have_posts() ) : ?>
<?php while ( $result->have_posts() ) : $result->the_post(); ?>
<?php the_permalink(); ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); endif; ?>