2011-12-19 54 views
0

在我的Index.php模板文件中,我有下面的代碼,它應該顯示一個特色帖子,如果它是粘性的,然後循環通過下面的其餘帖子。然而在第二個循環顯示任何東西和粘貼帖子不會出現...WordPress粘貼帖不顯示

該帖子已明確粘滯,第二個循環已成功隱藏它,但第一個循環只是不承認它:/它沒有設置私有並被標記爲已發佈。

任何人都可以看到代碼的任何問題?由於

<?php get_header(); ?> 
<div class="clearfix"> 
    <?php if(is_home()) { //if home then show the featured post and others ?> 
    <div class="main-column"> 
     <?php 
      $sticky = get_option('sticky_posts'); 
      $sticky_query = new WP_Query('p=' . $sticky[0]); 
      // The Loop 
      while ($sticky_query->have_posts()) : $sticky_query->the_post(); ?> 
       <div class="featured"> 
        <article class="clearfix"> 
         <h2><a href="<?php echo get_permalink() ?>"><?php echo $post->post_title ?></a></h2> 
         <p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p> 
         <?php if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it. 
          echo '<a href="'.get_permalink().'">'; 
          the_post_thumbnail(); 
          echo '</a>'; 
         } else { 
          echo '<a href="'.get_permalink().'"><img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" /></a>'; 
         }?> 
         <p><?php the_excerpt_max_charlength(300) ?></p> 
        </article> 
       </div> 
      <?php endwhile; ?> 

      <div class="articles"> 
      <?php $query = new WP_Query(array('post__not_in' => get_option('sticky_posts'), 'posts_per_page' => 10)); 
      $count = 0; 

      while ($query->have_posts()) : $query->the_post(); 
      $count++; ?> 
       <div <?php if($count % 2 == 0) echo 'class="no-margin"' ?>> 
        <article> 
         <h2><a href="<?php the_permalink(); ?>"><?php echo $post->post_title ?></a></h2> 
         <p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p> 
         <?php if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it. 
          echo '<a href="'.get_permalink().'">'; 
          the_post_thumbnail(); 
          echo '</a>'; 
         } else { 
          echo '<a href="'.get_permalink().'"><img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" /></a>'; 
         }?> 
         <p><?php the_excerpt_max_charlength(150) ?></p> 
        </article> 
       </div> 
      <?php if($count % 2 == 0) echo '<hr/>' ?> 
      <?php endwhile; ?> 
      </div> 

    <?php }else if(is_search() || is_category || is_tag()){ //if other than home (search, tag, category) then show normal list (no featured style) ?> 
    <div class="main-column search"> 
      <?php 
      $count = 0; 
      if(is_search()){ 
       echo '<h1>Search Results</h1>'; 
      } elseif(is_author()){ ?> 

<?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); ?> 

     <h1><?php echo $curauth->user_firstname; ?> <?php echo $curauth->user_lastname; ?></h1> 

    <div class="archive-meta"> 
     <p><?php echo $curauth->user_description; ?></p> 
    </div> 


      <?php } else if(is_category()){ ?> 


      <h1><?php single_cat_title('', true); ?></h1> 
      <?php 
       $category_description = category_description(); 
       if (! empty($category_description)) 
        echo '<div class="archive-meta">' . $category_description . '</div>'; 
      ?> 


      <?php }else if (is_tag()){ 
       echo '<h1>'.the_tag().'</h1>'; 
      } 
      if (have_posts()) : while (have_posts()) : the_post(); 
      $count++; ?> 
      <div> 
       <article> 
        <h2><a href="<?php the_permalink(); ?>"><?php echo $post->post_title ?></a></h2> 
        <p class="who-when">by <?php echo get_the_author_meta('first_name'); ?> <?php echo get_the_author_meta('last_name') ?> on <?php echo get_the_date() ?> </p> 
        <?php if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it. 
         echo '<a href="'.get_permalink().'">'; 
         the_post_thumbnail(); 
         echo '</a>'; 
        } else { 
         echo '<a href="'.get_permalink().'"><img src="'. get_bloginfo('template_directory') .'/images/thumb-placeholder.png" alt="Blog thumbnail" /></a>'; 
        }?> 
        <p><?php the_excerpt_max_charlength(300) ?></p> 
       </article> 
      </div> 
      <?php endwhile; else: ?> 
      <p>Sorry, no posts matched your criteria.</p> 
      <?php endif; ?> 
     <?php } ?> 
    </div> 
    <?php get_sidebar(); ?> 
</div> 
<?php get_footer(); ?> 
+0

你是如何將帖子標記爲粘滯的? – 2011-12-19 14:03:22

+0

我在這裏沒有看到任何錯誤。要開始調試,請嘗試回顯''sticky''變量以查看您獲得的內容。 – ronakg 2011-12-19 14:32:12

+0

@RonakG我得到一個錯誤'可捕獲的致命錯誤:類WP_Query的對象無法轉換爲字符串',如果我試着回顯, – Cameron 2011-12-19 17:36:13

回答

0

的代碼更改爲:

$sticky_query = new WP_Query('p=' . $sticky[1]);

修復這個問題,但我不知道爲什麼???