{"id":503319,"date":"2007-01-16T02:48:09","date_gmt":"2007-01-16T02:48:09","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/alternating-comment-backgrounds-with-ajax-comments\/"},"modified":"2016-08-18T22:20:54","modified_gmt":"2016-08-18T22:20:54","slug":"alternating-comment-backgrounds-with-ajax-comments","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/alternating-comment-backgrounds-with-ajax-comments\/","title":{"rendered":"Alternating comment backgrounds with AJAX Comments."},"content":{"rendered":"<p>I have been installing AJAX Comments and I love it, but it doesn&#8217;t support stylesheets that have alternating backgrounds (like the WordPress default!) in real-time (they&#8217;ll be fine after a refresh).<\/p>\n<p>As such, I decided to hack in the functionality myself.  Unfortunately, I&#8217;ve run into a small problem when it comes to understanding exactly what WordPress does with the comments array.  Currently, AJAX Comments gets each new comment&#8217;s formating by requesting a new comment page with one comment (the latest) from comments.php.  It then uses preg_match() to find the LI element and strip out the insides (class info and everything between the opening and closing tags).<\/p>\n<p>To make alternating work, I need to have WordPress create a comments page with <em>two<\/em> comments rather than one, and then strip out the class info from the second one.  Unfortunately, when I do so the second comment always seems to be blank.  Every other comment will update properly in wonderful AJAX style, but the ones in between (requiring a comment file with two comments) are blank until a refresh.<\/p>\n<p>Here&#8217;s the code I added to ajax-comments.php:<br \/>\n<code><br \/>\nif($commentcount%2 == 0) $comments = array($comment, $comment);<\/code><br \/>\nand:<br \/>\n<code>preg_match_all(&#039;#&lt;li(.*?)&gt;(.*)#ims&#039;, $commentout, $matches, PREG_SET_ORDER);<\/code><br \/>\nand:<br \/>\n<code>if($commentcount%2 == 0) echo &#039;&lt;li &#039;.$matches[1][1].&#039; style=&quot;display:none&quot;&gt;&#039;.$matches[1][2].&#039;&#039;;<\/code><\/p>\n<p>Using preg_match_all() instead of preg_match() should get all the instances of the LI element instead of just one&#8230;but it doesn&#8217;t.  I suspect that the problem occurs in the first line I added, and maybe WordPress won&#8217;t create a comment page with two of the same comment?<\/p>\n","protected":false},"template":"","class_list":["post-503319","topic","type-topic","status-closed","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/503319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/503319\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=503319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}