dougaodi8895 2015-12-14 14:10
浏览 13

为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> ';
  }
?>

I added this code for getting just single latest post on desired page.

How can I add next and previous button of posts to it?

  • 写回答

2条回答 默认 最新

  • doudu9148 2015-12-14 14:18
    关注

    If you want to get previous post link, You can do it using following code:

    <?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 } ?>
    

    If you want to get next post link, Than:

    <?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 } ?>
    

    You can check more information about this function here : get_adjacent_post()

    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上