2014-09-19 1 views
0

WordPress 코멘트 상자에 확인 이미지를 삽입해야합니다. 등급이 1/5, 2/5, 3/5, 4/5 인 경우 확인되지 않은 이미지가 표시되어야하고 등급이 5/5 인 경우 확인 된 이미지가 표시되어야합니다. 나는 많은 버전을 시도했지만 등급보다 강판 이하 5보다WordPress 코멘트 섹션에 등급 이미지를 삽입하는 방법은 무엇입니까?

내 코드 일 경우 모든 코멘트 상자에 한 번에 하나의 이미지를 표시 할 수 있었다 :

var code = $("<strong>5/5</strong>"); 
code.each(function() { 
    $('p.comment-rating').prepend('<p class="prepen_img"><img src="verified.png" /></p>'); 
}); 

내 코드 2 :

if ($("p.comment-rating:contains('2/5')").length !== 0) { 
    $('p.comment-rating').prepend('<p class="prepen_img"><img src="not-verified.png" /></p>'); 
} 
else { 
    $('p.comment-rating').prepend('<p class="prepen_img"><img src="verified.png" /></p>'); 
} 

내 코드 3 :

$('p.comment-rating').each(function() { 
    if ($("p.comment-rating:contains('5/5')").length !== 0) { 
     $('p.comment-rating').prepend('<p class="prepen_img"><img src="not-verified.png" /></p>'); 
    } 
    else { 
     $('p.comment-rating').prepend('<p class="prepen_img"><img src="verified.png" /></p>'); 
    } 
}); 

HTML :

,
<p class="comment-rating"> 
    <img src="2star.gif"> 
    <br> 
    Rating: <strong>2/5</strong> 
</p> 

위의 코드는 하나의 이미지 만 표시하며 else 조건은 작동하지 않습니다.

+0

당신이 누군가를 도와 주시겠습니까? –

+0

당신은 WordPress에 등급 시스템을 가지고 있으며 평가에 따라'not-verified' 또는'verified' 이미지를 옆에 표시하고 싶습니까? 당신이 '등급 : 2/5'에 표시된대로 html을 출력합니까? – Joonas

+0

@ Joonas, 물론 그렇습니다. 2/5가 등급 시스템의 산출입니다. –

답변

1

그래서 이런 일이 일할 수 :

jsfiddle Demo

HTML을 :

<p class="comment-rating"> 
    Rating: <strong>2/5</strong> 
</p> 

<p class="comment-rating"> 
    Rating: <strong>5/5</strong> 
</p> 

<p class="comment-rating"> 
    Rating: <strong>1/5</strong> 
</p> 

jQuery를 :

$(function() { 

    $('.comment-rating').each(function() { 

     var $this = $(this), 
      // Finds the first number inside '.comment-rating strong' before the forward slash and converts it to a number. 
      rating = parseInt($this.find('strong').text().split('/')[0]); 

     var verify = rating === 5 ? 'Verified' : 'Not-verified'; 

     $this.append('<span>'+ verify +'</span>'); 

    }); 

}); 

jsfiddle Demo

같은 :

나는 그런 이미지를 가지고 있지만, 여기에 내가 약간 9 호선 라인 (11)을 변경 이미지와 같은 일이 없기 때문에 텍스트 예, 더 명확 것 생각 html.

jQuery를 :

$(function() { 

    $('.comment-rating').each(function() { 

     var $this = $(this), 
      // Finds the first number inside '.comment-rating strong' before the forward slash and converts it to a number. 
      rating = parseInt($this.find('strong').text().split('/')[0]); 

     var verify = rating === 5 ? 'http://placekitten.com/g/20/30' : 'http://placekitten.com/20/30'; 

     $this.append('<img src="'+ verify +'" />'); 

    }); 

}); 
+0

와우 .. 너 놀랍다! 놀랄 만한!. 당신은 마술을 했어!. 그것의 잘 작동합니다. 많은 분들께 감사드립니다. 사랑하는 Jonnas .. 다시 감사드립니다. 제 코드가 작동하지 않는 이유를 설명해 주시겠습니까? –

+0

@KevinJohn 그래, 나는 왜 내가 가서 코드를 버렸는지 모르겠다. 어쨌든,이 문제는 [당신의'.each()'함수 안에서'$ (this) '를 사용하지 않았다는 사실에서 유래합니다 (http://jsfiddle.net/f52vp0zj/4/). 내 인생에서 나는 이것을 간단하게 설명 할 수없는 것처럼 보일 뿐이다. 그래서 나는 당신에게'console.log();'를 가지고 놀고, 크롬 개발자 도구 콘솔을 사용하여 그것이주는 가치. 예 : console.log ($ (this));'와'console.log ($ ('. comment-rating')); ' – Joonas

+0

설명 및 제안 주셔서 감사합니다. –

관련 문제