2014-06-17 4 views
-1

우선, 나쁜 영어로 유감스럽게 생각합니다.Wordpress : 한 페이지에 여러 (사용자 정의) 루프를 만드는 방법은 무엇입니까?

내가 원하는 이제

<?php $popular_restaurants = new WP_Query(array( 'post_type' => 'restaurant', 'posts_per_page' => 5, 'category_name' => 'restaurants', 'ignore_sticky_posts' => true, 'orderby' => 'comment_count', 'order' => 'DESC', 'date_query' => array( array( 'after' => '0', ), ), )); ?> <?php if ($popular_restaurants->have_posts()) : ?> <?php while ($popular_restaurants->have_posts()): $popular_restaurants->the_post(); ?> <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a> (<?php comments_number('Nu stemmen!', '1', '%'); ?>)</li> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else: ?> <p><?php _e('Sorry, er gaat even iets fout. We hebben niets kunnen vinden.'); ?></p> <?php endif; ?>

, 대신 대부분의 논평 게시물의 루프가 가장 높은 게시물을 투표를 보여줄 수 :

나는 특정 범주에 대한 대부분의 댓글을 달았 게시물을 보여 내 홈페이지에이 코드를 사용 . 어떻게 그렇게 할 수

/* Display likes/unlikes */ 
function aigoo_get_score() { 
    $comments = get_comments(array('post_id' => get_the_ID(),)); 
      $likes = array(); 
      $unlikes = array(); 
      foreach($comments as $comment) { 
       if('unlike' == $comment->comment_content) $unlikes[] = $comment; 
        else $likes[] = $comment; 
      } 
    $likescore = count($likes); 
    $unlikescore = count($unlikes); 
    $score = count($likes) - count($unlikes); 
    $totalvotes = count($likes) + count($unlikes); 
    return compact('likes', 'unlikes', 'likescore', 'unlikescore', 'score', 'totalvotes'); 
} 

대신의 게시물을 보여주는 :

$counts = aigoo_get_score(); 
extract($counts); //restores compacted variables 
<span>+'.$score.'</span> 

이 내 functions.php에 있습니다

일반적으로 나는 포스트 점수를 표시하려면이 코드를 사용 가장 많은 댓글을 달아 내 홈페이지에서 가장 높은 득표 수의 게시물을 표시합니까?

나는 그것이 가능 할지도 모른다. 잘만되면 누군가 나를 도와 줄 수 있습니다.

미리 감사드립니다.

+0

나는 이제 막 빠져 나갔다. 바로 여기 루프의 일부 문제가있다. 'showposts'는 가치가 떨어지고'posts_per_page '여야한다. '고양이'는 민달팽이 나 이름을 제외하고는 카테고리 ID 만 갖고 있지 않습니다. 'order'는 'ASC'또는 'DESC'여야합니다. ['WP_Query'] (http://codex.wordpress.org/Class_Reference/WP_Query) –

+0

당신이 제안한 변경을했습니다. 나는 cat을''cat '=> 1,''로 변경했다. 그게 맞습니까? 그리고''cat '=>'1 '이 아닌''그렇 겠지? 이제 첫 번째 루프와 기본 Wordpress 루프 만 작동하고 다른 두 개의 사용자 정의 루프는 작동하지 않습니다. 그들은 비어 있습니다. – edow

+0

귀하의 제안에 따라 업데이트 된 질문을 참조하십시오. 이제 첫 번째 루프에서 category_name을 다른 카테고리 이름으로 변경하려고했습니다. 그런 다음 게시물을 찾을 수 없다는 오류가 발생합니다. 문제가 있다고 생각합니다. 나는 URI에서 카테고리 슬러그를 시도했기 때문에 category_name은 괜찮을 것이다. – edow

답변

0

편집 됨 : 질문의 두 번째 부분에 대답 함. 이 코드는 테스트되지 않았으므로 방금 생각한대로 작성 했으므로 실수가있을 수 있습니다.

<?php $popular_restaurants = new WP_Query(array(
      'post_type'    => 'restaurant', 
      'posts_per_page'  => 5, 
      'category_name'   => 'restaurants', 
      'ignore_sticky_posts' => true, 
      'orderby'    => 'comment_count', 
      'order'     => 'DESC', 
      'date_query' => array(
       array(
        'after' => '0', 
       ), 
      ), 
     )); 
    $like_count_array = array(); ?> 
    <?php if ($popular_restaurants->have_posts()) : ?> 
    <?php while ($popular_restaurants->have_posts()): $popular_restaurants->the_post(); 
//Call your function inside the loop to get the like count, and assign the value to an array with the post id as the key.  
      $like_count = igoo_get_score(); 
      $id = get_the_ID(); 
      $like_count_array[$id] = $like_count['totalvotes']; ?> 
     <?php endwhile; ?> 
     <?php endif; ?> 
//run the loop four times to print the four highest values 
      <?php if (is_array($like_count_array)) { 
    for ($i = 1; $i <= 4; $i++) { 
      //Get the post id with the highest vote value 
      $post_id = array_search(max($like_count_array),$like_count_array); 
      //Remove this value from the array 
      unset($like_count_array[$post_id]); ?> 
      <li><a href="<?php echo get_permalink($post_id); ?>"> 
       <?php echo get_the_title($post_id); ?></a></li> 
     <?php } 
     }; ?> 
+0

답장을 보내 주셔서 감사합니다. 'wp_reset_postdata(); '를 사용하여 루프를 재설정하는 것은 불행하게도 작동하지 않습니다. 세 가지 목록에는 여전히 한 카테고리의 게시물 만 표시됩니다. 두 번째 코드에서 오류가 발생합니다. 페이지가 더 이상 완전히 보이지는 않지만 보겠습니다. – edow

+0

무엇이 오류입니까? –

+0

시도해보십시오. 대신 wp_reset_postdata(); –

0

때때로 wp_reset_postdata()가 예상대로 작동하지 않는 것 같습니다. 일반적으로 wp_reset_postdata() 문제는 내 루프 앞에 변수에 내 게시물 데이터를 저장하여 해결됩니다. 그런 다음 전역 $ post 변수를 저장된 게시물로 다시 설정하십시오. 이 같은.

$saved_post = $post; 
    <?php 
      $popular_restaurants = new WP_Query(array(
       'post_type'    => 'restaurant', 
       'posts_per_page'  => 5, 
       'category_name'   => 'restaurants', 
       'ignore_sticky_posts' => true, 
       'orderby'    => 'comment_count', 
       'order'     => 'DESC', 
       'date_query' => array(
        array(
         'after' => '0', 
        ), 
       ), 
      )); 
     ?> 
     <?php if ($popular_restaurants->have_posts()) : ?> 
     <?php while ($popular_restaurants->have_posts()): $popular_restaurants->the_post(); ?> 
      <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a> (<?php comments_number('Nu stemmen!', '1', '%'); ?>)</li> 
     <?php endwhile; ?> 
     <?php $post = $saved_post; ?> 
     <?php else: ?> 
     <p><?php _e('Sorry, er gaat even iets fout. We hebben niets kunnen vinden.'); ?></p> 
     <?php endif; ?> 

당신이 다시 원래의 $ 포스트 데이터 재설정 &을 저장 확실히 알고 이쪽으로.