返回列表 回复 发帖

comments.php文件循环语句?

我想在每个评论之前加上一个不同的图片
在comment.php文件里的        <?php foreach ($comments as $comment) : ?>下加入下面语句:

<?php if ($oodcomment == 'alt') { ?>
  <img class="corneralt" alt="" src="<?php bloginfo('template_directory'); ?>/images/ccalt.gif" /><br />
<?php } else if($oodcomment == '') { ?>
  <img class="corner" alt="" src="<?php bloginfo('template_directory'); ?>/images/cc.gif" /><br />
<?php } else { ?>
  <img alt="" src="<?php bloginfo('template_directory'); ?>/images/ccalthighlighted.gif" class="corneralthighlighted"/><br />
<?php } ?>

结果在每个评论前只输出中间那个:
<img class="corner" alt="" src="<?php bloginfo('template_directory'); ?>/images/cc.gif" /><br />
请大大们帮我看下,万分谢谢!
不明白你要实现什么功能

回复 #2 fvcity 的帖子

每个评论之前加上一个不同的图片
下面是循环代码,大大们帮看看
需要实现功能:在每条评论之前加上一个不同的图片
  1. <?php // Do not delete these lines
  2.         if ('comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
  3.                 die ('Please do not load this page directly. Thanks!');

  4.         if (!empty($post->post_password)) { // if there's a password
  5.                 if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie
  6.                         ?>

  7.                         <p class="nocomments">This post is password protected. Enter the password to view comments.</p>

  8.                         <?php
  9.                         return;
  10.                 }
  11.         }

  12.         /* This variable is for alternating comment background */
  13.         $oddcomment = 'alt';
  14. ?>

  15. <!-- You can start editing here. -->

  16. <?php if ($comments) : ?>
  17.   <h3 id="respond"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>

  18.         <div class="commentlist">

  19.         <?php foreach ($comments as $comment) : ?>

  20. <?php /* Changes every other comment to a different class */
  21. if ($comment->comment_author_email == get_the_author_email()) $oddcomment = 'alt althighlighted';
  22. else if ('alt' == $oddcomment || 'althighlighted' == $oddcomment) $oddcomment = '';
  23. else $oddcomment = 'alt';
  24. ?>


  25. <?php if ($oodcomment == 'alt') { ?>
  26.   <img class="corneralt" alt="" src="<?php bloginfo('template_directory'); ?>/images/ccalt.gif" />
  27. <?php } else if($oodcomment == '') { ?>
  28.   <img class="corner" alt="" src="<?php bloginfo('template_directory'); ?>/images/cc.gif" />
  29. <?php } else { ?>
  30.   <img alt="" src="<?php bloginfo('template_directory'); ?>/images/ccalthighlighted.gif" class="corneralthighlighted"/>
  31. <?php } ?>

  32.                 <div class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">
  33.       <abbr title="#">1</abbr>~
  34.       <b><span title="#"><?php comment_author_link() ?></span></b>
  35.       <a href="#comment-<?php comment_ID() ?>">#</a>

  36.                         
  37.                         <?php if ($comment->comment_approved == '0') : ?>
  38.                         <em>Your comment is awaiting moderation.</em>
  39.                         <?php endif; ?>
  40.                         <br />

  41.                         <?php comment_text() ?>

  42.                 </div>


  43.         <?php endforeach; /* end for each comment */ ?>
复制代码
我看你的代码写的比较复杂,
首先$oddcomment = 'alt';
然后你判断,如果留言的人的email是get_the_author_email,这个函数应该是获得本日志作者的email
$oddcomment = 'alt althighlighted';
也就是你自己留言后才会执行这里...

接着如果条件不成立
'alt' == $oddcomment || 'althighlighted' == $oddcomment
因为你的$oddcomment 一开始就是alt,所以这里你将他清空了...

是将'alt althighlighted'是一个字符串,但不是两个,你判断althighlighted又判断alt,这里是不对的
下面是即使你有一个为真那么又将$oddcomment付值为空...
结果你的$oddcomment一直是空的...

现在我想问问你,你是不是想,判断作者和普通留言的人还区分他的图标,说明一下你三个图标代表什么意思,不然没有办法帮...

[ 本帖最后由 fvcity 于 2008-1-25 12:00 PM 编辑 ]

回复 #5 fvcity 的帖子

多谢你的分析使我找到了原因,我又添加了个变量$i,当$oddcomment值变动时,$i也跟着变动,根据$i值来判断输出就可以了。

这样做目的是:当$oddcomment值分别是‘’;'alt';'author'时前面增加一个不同的图片,也就是为每条留言前增加一个不同棱角图片。

再次感谢fvcity  

下面是修改后可以实现功能的代码,也贴一下吧^_^
------------------------------------------------------------------------------
<code>
!-- You can start editing here. -->
<?php $i = 0; ?>
<?php if ($comments) : ?>
  <h3 id="respond"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>

        <div class="commentlist">

        <?php foreach ($comments as $comment) : ?>

<?php /* Changes every other comment to a different class */
if ($comment->comment_author_email == get_the_author_email()) { $oddcomment = 'alt althighlighted'; $i = 2; }
else if ('alt' == $oddcomment || 'althighlighted' == $oddcomment) { $oddcomment = ''; $i = 0; }
else { $oddcomment = 'alt'; $i = 1; }
?>


<?php if ($i == 1) { ?>
  <img class="corneralt" alt="" src="<?php bloginfo('template_directory'); ?>/images/ccalt.gif" />
<?php } else if($i == 0) { ?>
  <img class="corner" alt="" src="<?php bloginfo('template_directory'); ?>/images/cc.gif" />
<?php } else { ?>
  <img alt="" src="<?php bloginfo('template_directory'); ?>/images/ccalthighlighted.gif" class="corneralthighlighted"/>
<?php } ?>
</code>

[ 本帖最后由 jinzhizhu 于 2008-1-25 05:06 PM 编辑 ]
返回列表