wordpress调用文章上一篇 下一篇的标题和链接

[复制链接]
发表于 2025-12-21 15:01:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在WordPress中,你可以使用`get_adjacent_post()`函数来获取上一篇和下一篇文章的信息,并根据是否存在这些文章来表现相应的链接和提示。以下是一个示例代码,它会查抄上一篇和下一篇文章是否存在,并相应地表现链接或提示信息:
  1. <?php
  2. // Obtain the previous article
  3. $prev_post = get_adjacent_post(true, '', true);
  4. $prev_title = $prev_post ? $prev_post->post_title : '';
  5. $prev_link = $prev_post ? get_permalink($prev_post->ID) : '';
  6. // Get the next article
  7. $next_post = get_adjacent_post(true, '', false);
  8. $next_title = $next_post ? $next_post->post_title : '';
  9. $next_link = $next_post ? get_permalink($next_post->ID) : '';
  10. // wodepress.com Display the link and title of the previous article
  11. if ($prev_post) {
  12.     echo '<li><a href="' . esc_url($prev_link) . '" rel="prev">' . esc_html($prev_title) . '</a> 上一篇</li>';
  13. } else {
  14.     echo '<li>没有上一篇文章</li>';
  15. }
  16. // Display the link and title of the next article
  17. if ($next_post) {
  18.     echo '<li><a href="' . esc_url($next_link) . '" rel="next">' . esc_html($next_title) . '</
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表