10 求大佬教我怎么在文章页模板添加翻页代码。

attachments-2019-06-R7UJoLgd5cfb696e2b307.png

划线位置,也就是页面文章底部中间希望添加上   上一篇   下一篇按钮。
有的时候文章被删 文章ID 1。3。5。6这样,没有按顺序排列,也要正确跳转到下一篇,这种情况代码怎么添加?
跪求大佬帮助。

请先 登录 后评论

最佳答案 2019-06-16 10:00

我的思路如下:

1.在文章显示数据整理部分直接获取上一篇文章和下一篇文章内容,代码如下:

        /*获取上一篇文章*/
        $preArticle = Article::where("id","<",$article->id)->where("status",">",0)->orderBy("id",'asc')->first();
        /*获取下一篇文章*/
        $nextArticle = Article::where("id",">",$article->id)->where("status",">",0)->orderBy("id",'asc')->first();

然后设置上一篇和下一篇文章变量,这样view层就可以直接用了,代码如下:

        return view("theme::article.show")->with('article',$article)
                                          ->with('topUsers',$topUsers)
                                          ->with('relatedQuestions',$relatedQuestions)
                                          ->with('relatedArticles',$relatedArticles)
                                          ->with('preArticle',$preArticle) //新添加的上一篇文章
                                          ->with('nextArticle',$nextArticle); //新添加的下一篇文章

2.视图层逻辑,修改resources/views/themes/default/article/show.blade.php模板文件,加入下面的代码,样式自己调整。

            <div class="text-center">
                @if($preArticle) 上一篇:<a href="{{ route('blog.article.detail',['article_id'=>$preArticle->id]) }}">{{ $preArticle->title }}</a>@endif
                @if($nextArticle) 下一篇:<a href="{{ route('blog.article.detail',['article_id'=>$nextArticle->id]) }}">{{ $nextArticle->title }}</a>@endif
            </div>
请先 登录 后评论

其它 2 个回答

武汉丝丝网 - http://www.bjmrong.com

可以借鉴下其他平台的模板,这样自己利用起来方便多了,通常都是封装好了的。

请先 登录 后评论
君否先生

http://www.svipnet.com/index.php?c=category&id=6

请先 登录 后评论
  • 3 关注
  • 0 收藏,3518 浏览
  • 时光影子 提出于 2019-06-08 15:57

相似问题