2012-07-29 5 views
1

단일 게시물 모드 일 때 2 개의 최신 게시물을 반환하고 싶지만 현재 게시물은 제외하십시오. 그리고 나는 그것을했다. 문제는 단지 작동이 멈췄다는 것입니다. 그것은 코드를 변경하지 않았고 내 localhost뿐만 아니라 서버에서 멈추었습니다.wp_get_recent_posts가 작동을 멈췄습니다

<section id='recent_posts'> 

      <header class='recent_header'> 
       Recent posts 
      </header> 

      <?php 
       $id = $post->ID; 
       $recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id"); 

       foreach($recent_posts as $recent) { ?>       

        <article class='single_recent'> 
         <header> 
          <a href="<?php echo get_permalink($recent["ID"]); ?>"><?php echo $recent["post_title"]; ?></a> 
         </header> 
         <p> 
          <?php echo get_excerpt_by_id($recent["ID"]); ?> 
         </p> 
        </article> 

      <?php } ?> 

     </section> 

사람이 설명이 있습니까 : 여기

코드인가?

나는 인수를 제거하려고 시도했지만 아직 아무것도 시도하지 않았습니다. 빈 배열을 반환합니다.

동일한 효과를 얻기 위해 다른 기능을 사용해야하는 제안은 무엇입니까?

편집 :

<?php 

get_header(); 
get_sidebar(); 

?> 

     <?php the_post() ?> 

     <article class='post-single'> 

       <header class='post_header'> 

        <h1><?php the_title(); ?></h1> 

        <div class='post_header_bottom'> 
         <strong class='post_category'><?php echo get_the_category_list(', '); ?></strong> 
         <strong class='post_author'><span class='symbol'>U</span> by <?php the_author(); ?></strong> 
        </div> 

       </header> 

       <?php if (has_post_thumbnail()) : ?> 
       <figure class='post_single_image'> 
        <?php the_post_thumbnail(); ?> 
        <figcaption>No Will No Skill</figcaption> 
       </figure> 
       <?php endif; ?> 

       <div class='post_perex'> 
        <?php the_content(); ?> 
       </div> 

       <footer class='post_footer'> 

        <div class='post_footer_top'> 

         <div class='post_tags'> 
          <?php the_tags('', '', ''); ?> 
         </div> 

         <div class='post_time'> 
          <time datetime='<?php the_time('Y-m-d'); ?>' pubdate> 
           <span class='symbol'>P </span> 
           <?php relative_post_the_date(); ?> 
          </time> 
         </div> 

        </div> 

        <div class='post_share'> 

          <div class='share_show'> 
           <span class='symbol'>f</span> Like 
           | 
           <span class='symbol'>g</span> +1 
           | 
           <span class='symbol'>t</span> Tweet 

           <?php 
            if(function_exists('display_social4i')) 
             echo display_social4i("large","align-left"); 
           ?> 

          </div> 

         </div> 

       </footer> 

      </article> 

      <?php comments_template(); ?> 

      <section id='recent_posts'> 

       <header class='recent_header'> 
        Recent posts 
       </header> 

       <?php 
        global $post; 
        $id = $post->ID; 
        $qargs = array(
         'post__not_in'=> array($id), 
         'posts_per_page' => 2 
        ); 
        $recent_posts = new WP_Query($qargs); 

        if ($recent_posts->have_posts()) echo 'yes'; else echo 'nope'; 

        if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>       

         <article class='single_recent'> 
          <header> 
           <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
          </header> 
          <p> 
           <?php the_excerpt(); ?> 
          </p> 
         </article> 
       <?php endwhile;endif; ?> 
      </section> 

      <div class='space'></div> 
     </div> 


<?php 
get_footer(); 
?> 

답변

0

하는 당신은 당신의 루프를 누락하거나 현재 포스트 개체의 적어도 글로벌 인스턴스가 있습니다. 루프를 직접 사용하는 것을 선호하는 반면, 후자를 사용하면 빠져 나갈 수 있습니다.

변경 :

$id = $post->ID; 
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id"); 

사람 :

global $post; 
$id = $post->ID; 
$recent_posts = wp_get_recent_posts("numberposts=2&exclude=$id"); 

UPDATE :

루프은 query_posts 객체의 기본 정보를 잡기 위해 하나의보기에서 사용할 수 있습니다. 비록 하나의 게시물 일지라도, 루프는 the_content 및 기타 정보를 채우기 위해 가장 자주 사용됩니다.

wp_get_recent_posts()는 사용자 정의 게시 유형을 다루지 않는 한 wp_get_recent_posts()가 여전히 인수없이 결과를 반환해야하므로이 경우 ID가 관련이 없어야한다는 것이 맞습니다.

대체 솔루션은 WP_Query을 사용하여 최근의 게시물을 받고 시도하는 것입니다 :

<section id='recent_posts'> 

     <header class='recent_header'> 
      Recent posts 
     </header> 

     <?php 
      global $post; 
      $id = $post->ID; 
      $qargs = array(
       'post__not_in'=> array($id), 
       'posts_per_page' => 2 
      ); 
      $recent_posts = new WP_Query($qargs); 

      if($recent_posts->have_posts()) : while($recent_posts->have_posts()) : $recent_posts->the_post(); ?>       

       <article class='single_recent'> 
        <header> 
         <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
        </header> 
        <p> 
         <?php the_excerpt(); ?> 
        </p> 
       </article> 
     <?php endwhile;endif; ?> 
</section> 
WP_Query 표준 'ORDERBY'=> '날짜'와 '순서'=> 'DESC'매개 변수로 설정됩니다

, 따라서 명시 적으로 명시 할 필요는 없습니다 (단, 원하는 경우 추가 할 수 있습니다).

위에서 언급 한 것과 내 의견에 가장 최근 게시물 만 원한다면 또는 최근 맞춤 게시물 유형을 쿼리하려는 경우 확실하지 않습니다. 최근 게시물이 '게시'유형이고 원하는 게시물 인 경우 WP_Query의 'post_type'매개 변수와 wp_get_recent_posts의 기본값입니다.

$qargs = array(
    'post__not_in'=> array($id), 
    'posts_per_page' => 2, 
    'post_type' => array('post', 'custom_post_type_slug', ...) 
); 

그냥, 당신은 또한 당신이 원하는 경우 '전체'에 'post_type'을 설정할 수 있습니다 확인 :

그렇지 않으면, 당신은 $ qargs의 모습 있도록 명시 적으로 'post_type'매개 변수를 명시해야합니다 SOMETHING이 반환되는지 확인하십시오. 'post_type'이 'all'로 설정된 WP_Query 또는 wp_get_recent_posts에서 아무 것도 반환되지 않으면 문제가 훨씬 커지고 single.php 템플릿의 모든 코드를 확인해야합니다.

희망이 도움이됩니다.

NEW UPDATE :

가 모두와 대체 코드 블록을 주석보십시오 : 문제가 해결되지 않으면

wp_get_archives(array('type'=>'postbypost')); 

는, 나는 아이디어 중 신선한입니다. 파일 호스트가 끝날 무언가가 일어나서 아무것도 일어나지 않는 것을 설명 할 수 있습니다. 그들이 이런 종류의 활동에 대한 발표를했는지 확인하십시오. 나는 많은 파일 호스트가 PHP4 및 이전 버전의 MySQL을 교체하는 과정에 있으며 이는 예상치 못한 문제를 일으킬 수 있음을 알고 있습니다.

깨끗하고 별도로 설치 한 Wordpress와 테마 사본을 사용하여 새 하위 도메인을 만들려고합니다. 설정이 끝나면 새 게시물을 만들어 동일한 문제가 발생하는지 확인하십시오.

그렇다면 single.php 파일에서 get_sidebar에서 첫 번째로 <article>...</article> 블록을 주석 처리하여 코드 덩어리를 주석 처리하고 팝업 될 수있는 오류를 해결하십시오. 갑작스럽게 작업 한 코드 블록이 채워지기 시작하면 문제가 발생하지 않도록 코드 블록을 게시하십시오. 문제가 계속 발생하면 더 이상 함께 노력할 것입니다.

클린 설치로 문제가 해결되면 wp_options 테이블에서 일종의 불일치가 발생할 수 있습니다. 문제가 다시 나타날 때까지 테이블을 병합하기 시작할 것입니다 (첫 번째 wp_posts, wp_postmeta, wp_terms ....).

불행히도이 예외적 인 문제를 해결하기 위해 철저한 테스트가 필요할 수 있습니다. 죄송합니다. 순수한 코드 솔루션이 없습니다. 이것은 당신이 겪고있는 꽤 이상한 문제입니다. 계속 게시 해주세요. 제가 도와 드릴 수있는 일을하겠습니다.

+0

단일 포스트 뷰에서 루프를 사용하는 이유는 무엇입니까? BTW에 게시 ID가 문제가되지 않습니다. 신분증이 잘 잡히네. 문제는 함수가 0 개의 게시물을 반환한다는 것입니다 (매개 변수 없이도) –

+0

ID에 관계없이 함수가 여전히 결과 배열을 반환해야한다는 점이 맞습니다. 내 사과. 호기심에서 표준 게시물을 다루고 있습니까? 아니면 사용자 정의 게시물을 쿼리하려고합니까? 최근 맞춤 게시물 또는 페이지를 쿼리하려는 경우 'post_type'매개 변수를 설정해야하기 때문입니다. – maiorano84

+0

예, 표준 게시물을 반환하고 싶습니다. 귀하의 코드를 시도하고 여전히 아무것도 반환하지 않습니다. 게시물을 편집하고 전체 single.php 코드를 붙여 넣으려고합니다. –