WordPress 中的下一篇/上一篇文章导航

DDD
发布: 2024-12-18 10:36:02
原创
1366人浏览过

wordpress 中的下一篇/上一篇文章导航

许多博客和网站都采用“上一篇/下一篇”文章导航,方便读者浏览文章。通过“上一篇”和“下一篇”链接,读者可以轻松跳转到前后文章,无需返回主页或文章列表,提升用户体验和参与度。此导航通常显示相邻文章标题,鼓励读者继续阅读,延长网站访问时间。

以下WordPress代码片段实现了这一功能:

/* ---------------------------------------------------------- */
//                文章前后导航代码片段                   //
/* ---------------------------------------------------------- */
function sp_post_nav_shortcode( $atts ) {

  $atts = shortcode_atts( array(
      'type' => 'next', // 默认显示下一篇
  ), $atts, 'sp_post_nav' );

  // 获取上一篇或下一篇
  $post = $atts['type'] === 'prev' ? get_previous_post() : get_next_post();

  // 箭头图标类名
  $arrow_class = $atts['type'] === 'prev' ? 'fas fa-arrow-left' : 'fas fa-arrow-right';

    // 容器类名
    $link_class = $atts['type'] === 'prev' ? 'prev' : 'next';

  if ( $post ) {

      $post_title = get_the_title( $post );

      $sp_post_nav = '<a class="post-link ' . $link_class . '" href="' . esc_url( get_permalink( $post ) ) . '">';
      $sp_post_nav .= '<i class="' . $arrow_class . '"></i>';
      $sp_post_nav .= '<div class="inner-wrapper">';
      $sp_post_nav .= $atts['type'] === 'prev' ? '<p>上一篇文章:</p>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/xiazai/js/276">
                            <img src="https://img.php.cn/upload/jscode/000/000/001/58980a5a1bc27966.jpg" alt="jQuery选项卡风格下拉导航代码">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/xiazai/js/276">jQuery选项卡风格下拉导航代码</a>
                            <p>jQuery选项卡风格下拉导航代码,经常用于企业网站或者商城网站,鼠标放在导航栏上面,下面显示一些分类信息,黑色的效果,很大气美观,php中文网推荐下载!</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="jQuery选项卡风格下拉导航代码">
                                <span>152</span>
                            </div>
                        </div>
                        <a href="/xiazai/js/276" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="jQuery选项卡风格下拉导航代码">
                        </a>
                    </div>
                ' : '<p>下一篇文章:</p>';
      $sp_post_nav .= '<h6>' . $post_title . '</h6>';
      $sp_post_nav .= '</div>';
      $sp_post_nav .= '</a>';

      return $sp_post_nav;
  }

  return '';
}
add_shortcode( 'sp_post_nav', 'sp_post_nav_shortcode' );
登录后复制

完整代码及更多信息,请访问:https://www.php.cn/link/9afeba42972a726b6b6d46ed9d506e99 (Snippflow WordPress 代码片段)

以上就是WordPress 中的下一篇/上一篇文章导航的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号