2017-10-28 51 views
0

)我有一個稱爲短期課程的自定義文章類型,並且我已添加一個自定義分類標準以稱爲課程類型,它使我可以對課程進行分類...在頁面模板中顯示特定分類(

我試圖創建一個頁面模板,將顯示一個單一的分類(所有類別)的所有課程。在這種情況下,我想說明,已分配的分類(類)所有課程的Adobe ...香港專業教育學院嘗試了一些不同的東西,但是我的繼承人最新代碼:

<?php 

// get the custom post type's taxonomy terms 

$custom_taxterms = wp_get_object_terms($post->ID, 'adobe', array('fields' => 'ids')); 
// arguments 
$args = array(
'post_type' => 'short_courses', 
'post_status' => 'publish', 
'posts_per_page' => 20, // you may edit this number 
'orderby' => 'rand', 
'tax_query' => array(
    array(
     'taxonomy' => 'adobe', 
     'field' => 'id', 
     'terms' => $custom_taxterms 
    ) 
), 
'post__not_in' => array ($post->ID), 
); 
$related_items = new WP_Query($args); 

// loop over query 
if ($related_items->have_posts()) : 
    echo '<ul>'; 
    while ($related_items->have_posts()) : $related_items->the_post(); 
    ?> 
     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
    <?php 
    endwhile; 
    echo '</ul>'; 
endif; 
// Reset Post Data 
wp_reset_postdata(); 
?> 

它不通過任何東西拉。 ..

我曾嘗試使用此代碼太:

<?php 
    $args = array( 
     'post_type' => 'short_courses', 
     'orderby' => 'title', 
     'order' => 'ASC', 
     'product_categories' => 'adobe' 
    ); 
    $the_query = new WP_Query($args); 

?> 

    <?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> 


     <?php 
     $thumbnail_id = get_post_thumbnail_id(); 
     $thumbnail_url = wp_get_attachment_image_src($thumbnail_id, 'thumbnail-size', true); 
     ?> 

     <p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p> 
     <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>  


    <?php $products_count = $the_query->current_post + 1; ?> 
    <?php if ($products_count % 4 == 0): ?> 

    <?php endif; ?> 


    <?php endwhile; endif; ?> 

但這個代碼拉通過所有的短期課程,而不僅僅是那些的Adobe ...

任何人都可以電話米我要去哪裏?

回答

0

好吧,我設法解決這個問題 - 這只是一個小錯誤。在上面的第二批代碼中,說product_categories的部分需要更改爲course_type!因此,繼承人的新的工作代碼:

<?php 
     $args = array( 
      'post_type' => 'short_courses', 
      'orderby' => 'title', 
      'order' => 'ASC', 
      'course_type' => 'adobe' 
     ); 
     $the_query = new WP_Query($args); 

    ?> 

     <?php if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> 


      <?php 
      $thumbnail_id = get_post_thumbnail_id(); 
      $thumbnail_url = wp_get_attachment_image_src($thumbnail_id, 'thumbnail-size', true); 
      ?> 

      <p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p> 
      <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>  


     <?php $products_count = $the_query->current_post + 1; ?> 
     <?php if ($products_count % 4 == 0): ?> 

     <?php endif; ?> 


     <?php endwhile; endif; ?> 
1

你注意到了這條線嗎?

<?php if (have_posts()) : while 

,你可以把它改成

<?php if ($the_query->have_posts()) : while 

,看看是否應用了過濾器? WordPress具有全球職位功能,例如get_the_title(),這可能會干擾您的自定義查詢對象,例如您正在使用的查詢對象$ the_query

我還建議您在此之前刪除if聲明以提高代碼的可讀性。 while循環不會在頁面上產生任何輸出,如果查詢沒有任何帖子,那麼需要if語句。

整個代碼就變成

<?php 
     $args = array( 
      'post_type' => 'short_courses', 
      'orderby' => 'title', 
      'order' => 'ASC', 
      'product_categories' => 'adobe' 
     ); 
     $the_query = new WP_Query($args); 

    ?> 

     <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 


      <?php 
      $thumbnail_id = get_post_thumbnail_id(); 
      $thumbnail_url = wp_get_attachment_image_src($thumbnail_id, 'thumbnail-size', true); 
      ?> 

      <p><a href="<?php the_permalink(); ?>"><img src="<?php the_field('image'); ?>" alt="<?php the_title();?> graphic"></a></p> 
      <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>  


     <?php $products_count = $the_query->current_post + 1; ?> 
     <?php if ($products_count % 4 == 0): ?> 

     <?php endif; ?> 


     <?php endwhile; ?> 

請確保該分類名稱您正在使用「product_categories」 =>「土坯」是你在register_taxonomy功能,即使用相同的一個確保你有register_taxonomy('product_categories',array('short_courses'),$ args);如果您還有其他產品類別,請在查詢中使用它。希望這可以幫助。

+0

似乎並不具有唉效果恐怕@Nkole - 我已經更新了我的代碼,但增加了... –

+0

高興你得到它的工作。那些可能令人沮喪! –

相關問題