本文章來(lái)給大家介紹一下Typecho實(shí)現(xiàn)評(píng)論無(wú)限嵌套顯示實(shí)例,希望此方法對(duì)各位同學(xué)會(huì)有所幫助,好吧,寫下這個(gè)題目我就覺(jué)得好像又沒(méi)什么可說(shuō)的,所以我估計(jì)會(huì)寫的很簡(jiǎn)略,誰(shuí)叫我就是個(gè)懶胖子呢.
評(píng)論列表的輸出,官方的是下面這個(gè)樣子的,代碼如下:
<?php $comments->listComments(); ?>
官方的輸出在定義CSS的時(shí)候有點(diǎn)別扭,所以很多主題都用到了螞蚱的那篇《自定義評(píng)論列表的樣式》中的方法(螞蚱是大神啊~~).
接下來(lái)的內(nèi)容是基于螞蚱的代碼,嗯,廢話了這么多,先上一下效果,就是下圖這個(gè)樣子的,第一次回復(fù)縮進(jìn),第二層之后便不再縮進(jìn),保持對(duì)齊.

下面說(shuō)實(shí)現(xiàn)方法,首先看螞蚱原來(lái)的一段代碼,代碼如下:
- <?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;
- ?>">
這一段是判斷評(píng)論 ID,父級(jí)評(píng)論還是子級(jí)評(píng)論以及判斷評(píng)論 ID 的奇偶數(shù)什么的,其實(shí)就在子評(píng)論部分加一層深度的判斷就可以了,修改后的代碼如下:
- <?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;
- ?>">
其實(shí)就是一句話的事,就是加了個(gè)判斷,子評(píng)論中是否深度超過(guò)了2,然后給一個(gè)不同的id來(lái)定義樣式,我還假裝像模像樣的搞了篇文章出來(lái),好吧,我就是個(gè)水貨.
新聞熱點(diǎn)
疑難解答