繁体   English   中英

为wordpress中的帖子设置上一个和下一个

[英]Set Previous and next for the posts in wordpress

<?php
  $args = array( 'numberposts' => '1' );
  $recent_posts = wp_get_recent_posts( $args );
  foreach( $recent_posts as $recent ){
    echo '<h1><a href="' . get_permalink($recent["ID"]) .'">' .$recent["post_title"].'</a> </h1> ';
  }
?>

我添加了此代码,以便仅在所需页面上获得单个最新帖子。

如何添加帖子的下一个和上一个按钮?

如果要获取previous post链接,可以使用以下代码进行操作:

<?php $prev_post = get_adjacent_post( false, '', true ); ?>
 <?php if ( is_a( $prev_post, 'WP_Post' ) ) { ?>
    <a href="<?php echo get_permalink( $prev_post->ID ); ?>"><?php echo get_the_title( $prev_post->ID ); ?></a>
 <?php } ?>

如果您想获得next post链接,请:

<?php $next_post = get_adjacent_post( false, '', false ); ?>
 <?php if ( is_a( $next_post, 'WP_Post' ) ) {  ?>
    <a href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo get_the_title( $next_post->ID ); ?></a>
 <?php } ?>

您可以在此处查看有关此功能的更多信息: get_adjacent_post()

             $previous=$recent["ID"]-1;
             echo '<a href="' . get_permalink($previous) . '">' .   PREVIOUS.'</a> '; 

             $next=$recent["ID"]+1;
             echo '<a href="' . get_permalink($next) . '">' .   NEXT.'</a> ';

我做到了,它奏效了:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM