2014-04-04 4 views
0

지난 5 시간 동안 내 머리를 찢어 내고 있습니다. 특정 분류에서 내 맞춤 게시물을 표시하고 페이지 템플릿에서 실행중인 코드를 여러 루프로 사용하려고하지만 제목, 발췌 및 영구 링크가 페이지 자체를 가리키며 내 맞춤 게시물이 아닙니다. 루프.사용자 정의 포스트 루프가 올바른 정보를 표시하지 않는 페이지 템플릿

  1. 나는 올바른 게시물이 get_posts($args)에서 반환되는 여부를 확인하기 위해 내 함수 내에서 print_r($posts)를 사용, 그것은 등 게시물과 콘텐츠의 제목, 즉 올바른 정보를 보여줍니다

    은 무엇 혼란 것은이다

  2. 게시물의 이 표시됩니다.
  3. 게시물이없는 경우 if(have_posts($posts))에 아무 것도 표시되지 않습니다.

마치 페이지 템플릿의 ID가 내 사용자 정의 루프를 덮어 쓰는 것처럼 보입니다. 완전히 루피하기 전에 문제가 무엇인지 알 수 있습니까?

감사

오스

/* ****************************************************************** */ 
    /* !BOX LAYOUT */ 
/* ****************************************************************** */ 

// Function to create boxes 
function osu_fbox($day, $cpt, $tax, $term) { 

    // Filter posts 
    $args = array(
        'post_type'   => $cpt, 
        'posts_per_page' => -1, 
        'tax_query'   => array(
         array(
          'taxonomy' => $tax, 
          'field'  => 'slug', 
          'terms'  => $term 
         ) 
        ) 
       ); 
    $posts = get_posts($args); 

    print_r($posts); 

    // Display content for this box 
    echo '<p class="fheading">' . $day .'</p>'; 
    if (have_posts($posts)) : 
     foreach ($posts as $post) : setup_postdata($post); ?> 

     <div id="lk-<?php echo sanitize_title($day); ?>-post-<?php the_ID(); ?>"> 
      <h2><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'literarykitchen'), the_title_attribute('echo=0')); ?>" rel="bookmark" class="readmore"><?php the_title(); ?></a></h2> 
      <?php the_excerpt(); ?> 
      <a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('Permalink to %s', 'literarykitchen'), the_title_attribute('echo=0')); ?>" rel="bookmark" class="readmore">Read more...</a> 
     </div> 
     <?php endforeach; ?> 
    <?php else: ?> 
     <p>No festival items currently scheduled</p> 
    <?php endif; wp_reset_query(); // Reset query so we can go again 
} 


// ------------------------------------------------------------------ 
// // !BOXES 
// ------------------------------------------------------------------ 

// CPT and taxonomy 
$cpt   = 'lkfestival'; 
$tax   = 'lkdays'; 

// Start if password protected 
if (post_password_required()) : ?> 
    <p>We are currently working on the festivals area.</p> 
    <p>Please check back soon</p> 
    <?php echo get_the_password_form(); ?> 

<?php else: ?> 

    <div id="weekdays" class="fcol"> 
     <?php echo osu_fbox('Monday', $cpt, $tax, 'monday'); ?> 
     <?php echo osu_fbox('Tuesday', $cpt, $tax, 'tuesday'); ?> 
     <?php echo osu_fbox('Wednesday', $cpt, $tax, 'wednesday'); ?> 
     <?php echo osu_fbox('Thursday', $cpt, $tax, 'thursday'); ?> 
     <?php echo osu_fbox('Friday', $cpt, $tax, 'friday'); ?> 
    </div> <!-- End #weekdays --> 

    <div id="saturday" class="fcol"> 
     <?php echo osu_fbox('Saturday', $cpt, $tax, 'saturday'); ?>   
    </div> <!-- End #saturday --> 

    <div id="sunday" class="fcol"> 
     <?php echo osu_fbox('Sunday', $cpt, $tax, 'sunday'); ?> 
    </div> <!-- End #sunday --> 

<?php endif; /* End if post password protected */ ?> 

답변

0

좋아, 나는 그것을 작동하게하는, 그래서 여기에 대한 답변을 게시합니다 방법을 알아 냈어. 그러나 나는 라고 생각한다.get_posts() 루프 내에서 the_title();the_excerpt();과 같은 일반 WP 템플릿 태그를 사용할 수 있어야합니까? 이 방법을 올바르게 사용하고 있습니까? 아니면이 대답을 개선 할 수 있습니까?

감사

오스

/* ****************************************************************** */ 
    /* !BOX LAYOUT */ 
/* ****************************************************************** */ 

// Function to create boxes 
function osu_fbox($day, $cpt, $tax, $term) { 

    // Filter posts 
    $args = array(
        'post_type'   => $cpt, 
        'posts_per_page' => -1, 
        'tax_query'   => array(
         array(
          'taxonomy' => $tax, 
          'field'  => 'slug', 
          'terms'  => $term 
         ) 
        ) 
       ); 
    $posts = get_posts($args); 

    // print_r($posts); 
    // echo 'COUNT: ' . count($posts); 

    // Display content for this box 
    echo '<p class="fheading">' . $day .'</p>'; 
    if (count($posts) > 0) : 
     foreach ($posts as $post) : setup_postdata($post); 
     $id   = $post->ID; 
     $title  = $post->post_title; 
     $excerpt = $post->post_excerpt; 
     $pl   = get_permalink($post->ID); 
     ?> 

     <div id="lk-<?php echo sanitize_title($day); ?>-post-<?php echo $id; ?>"> 
      <h2><a href="<?php echo $pl; ?>" title="<?php printf(esc_attr__('Permalink to %s', 'literarykitchen'), the_title_attribute('echo=0')); ?>" rel="bookmark" class="readmore"><?php echo $title; ?></a></h2> 
      <?php echo $excerpt; ?> 
      <a href="<?php echo $pl; ?>" title="<?php printf(esc_attr__('Permalink to %s', 'literarykitchen'), the_title_attribute('echo=0')); ?>" rel="bookmark" class="readmore">Read more...</a> 
     </div> 
     <?php endforeach; ?> 
    <?php else: ?> 
     <p>No festival items currently scheduled</p> 
    <?php endif; wp_reset_query(); // Reset query so we can go again 
} 


// ------------------------------------------------------------------ 
// // !BOXES 
// ------------------------------------------------------------------ 

// CPT and taxonomy 
$cpt   = 'lkfestival'; 
$tax   = 'lkdays'; 

// Start if password protected 
if (post_password_required()) : ?> 

    <p>We are currently working on the festivals area.</p> 
    <p>Please check back soon</p> 
    <?php echo get_the_password_form(); ?> 

<?php else: ?> 

    <div id="weekdays" class="fcol"> 
     <?php echo osu_fbox('Monday', $cpt, $tax, 'monday'); ?> 
     <?php echo osu_fbox('Tuesday', $cpt, $tax, 'tuesday'); ?> 
     <?php echo osu_fbox('Wednesday', $cpt, $tax, 'wednesday'); ?> 
     <?php echo osu_fbox('Thursday', $cpt, $tax, 'thursday'); ?> 
     <?php echo osu_fbox('Friday', $cpt, $tax, 'friday'); ?> 
    </div> <!-- End #weekdays --> 

    <div id="saturday" class="fcol"> 
     <?php echo osu_fbox('Saturday', $cpt, $tax, 'saturday'); ?>   
    </div> <!-- End #saturday --> 

    <div id="sunday" class="fcol"> 
     <?php echo osu_fbox('Sunday', $cpt, $tax, 'sunday'); ?> 
    </div> <!-- End #sunday --> 

<?php endif; /* End if post password protected */ ?> 
1

당신은 사용자 정의 포스트 기능 (the_title(), the_content(), ...)로 전화하기 전에 어딘가에 wp_reset_query()을 넣어해야합니다. 워드 프레스 사본에서

:

wp_reset_query()는 원래 메인 쿼리에 $의 wp_query 및 글로벌 포스트 데이터를 복원합니다. 이 함수는 query_posts() 함수를 사용해야 할 경우 query_posts() 뒤에 호출해야합니다. 아래 예제에서 언급했듯이 쿼리를 만들기 전에 pre_get_posts 필터를 사용하여 쿼리 매개 변수를 변경하는 것이 좋습니다.

http://codex.wordpress.org/Function_Reference/wp_reset_query