本文章來給大家介紹一下Typecho實現評論無限嵌套顯示實例,希望此方法對各位同學會有所幫助,好吧,寫下這個題目我就覺得好像又沒什么可說的,所以我估計會寫的很簡略,誰叫我就是個懶胖子呢.
評論列表的輸出,官方的是下面這個樣子的,代碼如下:
<?php $comments->listComments(); ?>
官方的輸出在定義CSS的時候有點別扭,所以很多主題都用到了螞蚱的那篇《自定義評論列表的樣式》中的方法(螞蚱是大神啊~~).
接下來的內容是基于螞蚱的代碼,嗯,廢話了這么多,先上一下效果,就是下圖這個樣子的,第一次回復縮進,第二層之后便不再縮進,保持對齊.
下面說實現方法,首先看螞蚱原來的一段代碼,代碼如下:
- <?php function threadedComments($comments, $options) {
- $commentClass = '';
- if ($comments->authorId) {
- if ($comments->authorId == $comments->ownerId) {
- $commentClass .= ' comment-by-author';
- } else {
- $commentClass .= ' comment-by-user';
- }
- }
- $commentLevelClass = $comments->_levels > 0 ? ' comment-child' : ' comment-parent';
- ?>
- <?php } ?>
- <li id="li-<?php $comments->theId(); ?>" class="comment-body<?php
- if ($comments->_levels > 0) {
- echo ' comment-child';
- $comments->levelsAlt(' comment-level-odd', ' comment-level-even');//開源軟件:Vevb.com
- } else {
- echo ' comment-parent';
- }
- $comments->alt(' comment-odd', ' comment-even');
- echo $commentClass;
- ?>">
這一段是判斷評論 ID,父級評論還是子級評論以及判斷評論 ID 的奇偶數什么的,其實就在子評論部分加一層深度的判斷就可以了,修改后的代碼如下:
- <?php function threadedComments($comments, $options) {
- $commentClass = '';
- if ($comments->authorId) {
- if ($comments->authorId == $comments->ownerId) {
- $commentClass .= ' comment-by-author';
- } else {
- $commentClass .= ' comment-by-user';
- }
- }
- $commentLevelClass = $comments->_levels > 0 ? ' comment-child' : ' comment-parent';
- $depth = $comments->levels +1; //添加的一句
- ?>
- <?php } ?>
- <li id="li-<?php $comments->theId(); ?>" class="comment-body<?php
- if ( $depth > 1 && $depth < 3 ) { //此處的判斷要修改
- echo ' comment-child';
- $comments->levelsAlt(' comment-level-odd', ' comment-level-even');
- }
- elseif ( $depth > 2 ) {
- echo ' comment-child2';
- $comments->levelsAlt(' comment-level-odd', ' comment-level-even');
- }
- else {
- echo ' comment-parent';
- }
- $comments->alt(' comment-odd', ' comment-even');
- echo $commentClass;
- ?>">
其實就是一句話的事,就是加了個判斷,子評論中是否深度超過了2,然后給一個不同的id來定義樣式,我還假裝像模像樣的搞了篇文章出來,好吧,我就是個水貨.
新聞熱點
疑難解答