2017-10-09 22 views
0

所以基本上,我正在一個自定義的WordPress主題。我想要做的是爲每個類別設置一個圖標。如果循環開始並且帖子有一個類別,它將顯示它已經分配的圖標。現在它顯示正確的圖標,但標題和exerpt不斷更改爲頁面的名稱。這裏有一個例子,我有三個職位的數學,英語和歷史,他們都有正確的圖標,但顯示名稱博客帖子頁面,而不是數學,英語或歷史。如何使類別圖標的PHP後循環

<?php /* Template Name: News Blog Page */ get_header(); ?> 
<div id="blog-post-wrapper" class="section_wrapper"> 
<div class="column three-fourth"> 
    <?php $currentPage = get_query_var('paged'); 
     $args = array(
      'post_type' => 'post', 
      'order' => 'DESC', 
      'posts_per_page' => 9, 
      'paged' => $currentPage 
     ); 
     $the_query = new WP_Query($args); 
     if($the_query -> have_posts()): 
      while ($the_query -> have_posts()): $the_query -> the_post(); 
       get_template_part('postloopcontent', get_post_format()); 
      endwhile; 
     echo "<div class='pagination'>"; 
      echo paginate_links(array(
       'total' => $the_query -> max_num_pages 
      )); 
     echo "</div>"; 
     endif; 
    ?> 
</div> 
<div class="column one-fourth"> 
    <?php get_sidebar(); ?> 
</div> 
</div> 
<?php get_footer(); ?> 

最重要的是我的基本佈局,它抓住了我的循環。下一個是我的循環

<?php 

// Standard Post Format 

?> 



    <?php $bgImage = get_the_post_thumbnail_url(); ?> 
    <div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);"> 

      <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img"> 
       <?php 

       // Find the first category the post is in. 
       $categories = get_the_category(); 
       $category = $categories[ 0 ]->term_id; 

       $imgargs = array(
        'cat' => $category, 
        'post_status' => 'inherit', 
        'post_type' => 'attachment', 
        'posts_per_page' => '1' 
       ); 

       $imgquery = new WP_Query($imgargs); 

      if ($imgquery->have_posts()) { 
       while ($imgquery->have_posts()) { $imgquery->the_post(); ?> 
        <div class="category-featured-image"> 
         <?php echo wp_get_attachment_image($post->ID, 'thumbnail'); ?> 
        </div> 
        <?php 
       } 
      } 
      // Reset postdata to restore ordinal query. 
      wp_reset_postdata(); 

     ?> 
     </a> 
    <div id="content-box"> 
     <h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1> 
     <?php the_excerpt(); ?> 
    </div> 
</div> 

回答

0

在你的循環文件,你在休息後的數據即wp_reset_postdata();$imgquery循環/境外。如果你可以將postdata rest函數封裝在條件中,我認爲這應該起作用。

你的代碼必須看起來像這樣

<?php 

// Standard Post Format 

?> 



    <?php $bgImage = get_the_post_thumbnail_url(); ?> 
    <div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);"> 

      <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img"> 
       <?php 

       // Find the first category the post is in. 
       $categories = get_the_category(); 
       $category = $categories[ 0 ]->term_id; 

       $imgargs = array(
        'cat' => $category, 
        'post_status' => 'inherit', 
        'post_type' => 'attachment', 
        'posts_per_page' => '1' 
       ); 

       $imgquery = new WP_Query($imgargs); 

      if ($imgquery->have_posts()) { 
       while ($imgquery->have_posts()) { $imgquery->the_post(); ?> 
        <div class="category-featured-image"> 
         <?php echo wp_get_attachment_image($post->ID, 'thumbnail'); ?> 
        </div> 
        <?php 
       } 
       // Reset postdata to restore ordinal query. 
       wp_reset_postdata(); 
      } 

     ?> 
     </a> 
    <div id="content-box"> 
     <h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1> 
     <?php the_excerpt(); ?> 
    </div> 
</div> 
+0

好感謝你的最後一個部分,變化沒有工作,但現在的問題是這樣的:

固定鏈接抓取的鏈接當前頁面和標題是完全一樣的東西。因此,不是顯示帖子的鏈接或標題,而是顯示當前頁面和標題的鏈接。 –