2015-07-28 112 views
1

我已經爲具有兩個自定義分類法(「顏色」和「樣式」)的產品創建了自定義帖子類型。產品在這兩個標籤。當你進入分類術語頁面時,它應該像它應該那樣工作,即如果你去/colour/blue它會列出所有的藍色產品。在Wordpress中排序自定義分類術語頁面

我想要發生的是當你去/colour/blue是列出所有的藍色產品,但他們通過第二個分類「風格」分組顯示前三個產品閱讀更多鏈接。

所以

藍色>產品類型1

  • 產品1
  • 產品2
  • 產品3

查看更多..

藍>產品類型2

  • 產品3
  • 產品4
  • 產品5

更多詳情

有誰知道這是可能?

當前的代碼是這樣,我已經創造了術語分類法colour.php

<?php get_header(); ?> 

<main role="main" class="blinds"> 

    <h1 class="main-product-title">Products</h1> 

    <!-- section --> 
    <section class="main-product-content"> 

     <h2><?php $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); echo $term->name; ?></h2> 

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

      <!-- article --> 
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
       <!-- post image --> 
       <?php if (has_post_thumbnail()) : // Check if thumbnail exists ?> 

         <?php the_post_thumbnail(array(720,400)); // Declare pixel size you need inside the array ?> 

       <?php endif; ?> 
       <!-- /post thumbnail --> 

       <!-- post title --> 
       <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4> 
       <!-- /post title --> 

       <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">View ></a></p> 

      </article> 

      <!-- /article --> 

     <?php endwhile; ?> 

     <?php else: ?> 

      <!-- article --> 
      <article> 
       <h2><?php _e('Sorry, nothing to display.', 'html5blank'); ?></h2> 
      </article> 
      <!-- /article --> 

     <?php endif; ?> 

     <?php get_template_part('pagination'); ?> 

    </section> 
    <!-- /section --> 

    <div class="sidebar-wrapper"> 
     <?php if(!function_exists('dynamic_sidebar') || !dynamic_sidebar('product-sidebar')) ?> 
    </div> 

</main> 
+0

你有什麼代碼,使遠嗎? – vard

+0

您好,我已經爲taxonomy-colour.php創建了一個分類模板,並將其放在上面。 – user1837290

+0

我以前回答過類似的問題,無論是在這裏還是在[wordpress.se],都不記得在哪裏,但實質上,您需要使用'the_posts'過濾器並使用'usort()'來提交帖子。 –

回答

0

正如評論中所述,您需要在the_posts過濾器上使用usort()來根據需要對帖子進行排序。

你可以嘗試以下方法:(注:下面的代碼是未經測試,並且需要PHP 5.4+由於數組語法

add_filter('the_posts', function ($posts, $q) 
{ 
    if ( $q->is_main_query() // Target only the main query 
      && $q->is_tax('colour') // Only target the colour taxonomy term pages 
    ) { 
     /** 
     * There is a bug in usort that will most probably never get fixed. In some instances 
     * the following PHP warning is displayed 

     * usort(): Array was modified by the user comparison function 
     * @see https://bugs.php.net/bug.php?id=50688 

     * The only workaround is to suppress the error reporting 
     * by using the @ sign before usort 
     */  
     @usort($posts, function ($a, $b) 
     { 
      // Use term name for sorting. We will sort by style taxonomy term 
      $array_a = get_the_terms($a->ID, 'style'); 
      $array_b = get_the_terms($b->ID, 'style'); 

      // Add protection if posts don't have any terms, add them last in queue 
      if (empty($array_a) || is_wp_error($array_a)) { 
       $array_a = 'zzz'; // Make sure to add posts without terms last 
      } else { 
       $array_a = $array_a[0]->name; 
      } 

      // Add protection if posts don't have any terms, add them last in queue 
      if (empty($array_b) || is_wp_error($array_b)) { 
       $array_b = 'zzz'; // Make sure to add posts without terms last 
      } else { 
       $array_b = $array_b[0]->name; 
      } 

      /** 
      * Sort by term name, if term name is the same sort by post date 
      * You can adjust this to sort by post title or any other WP_Post property_exists 
      */ 
      if ($array_a != $array_b) { 
       // Choose the one sorting order that fits your needs 
       return strcasecmp($array_a, $array_b); // Sort term alphabetical ASC 
       //return strcasecmp($array_b, $array_a); // Sort term alphabetical DESC 
      } else { 
       return $a->post_date < $b->post_date; // Not sure about the comparitor, also try > 
      } 
     }); 
    } 
    return $posts; 
}, 10, 2); 
+0

您應該用新信息開始一個新問題。限制您的帖子與此代碼無關,您需要使用'pre_get_posts'過濾主要查詢。 –

+0

會這樣做,並且再次感謝您對此的幫助。 – user1837290

-1

的第一件事情做一個分類模板獲得可用風格描述您的當前分類術語。爲此,您可以獲取當前分類標準的所有帖子ID,然後使用wp_get_object_terms獲取款式條款。然後你循環瀏覽它們,查詢具有這兩個分類術語(顏色風格)的三個帖子。

這裏是你可以使用你的情況的一個例子:

<?php 
// get the term... 
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy')); 

// get all posts from this term and store the IDs 
$term_posts = get_posts(array('tax_query' => array(array(
    'taxonomy' => get_query_var('taxonomy'), 
    'terms' => $term->ID 
)))); 
$term_posts_IDs = array(); 
foreach ($term_posts as $term_post) { 
    $term_posts_IDs[] = $term_post->ID; 
} 

// get the available styles from this set of posts 
$styles = wp_get_object_terms($term_posts_IDs, 'style'); 

?> 
<h2><?php echo $term->name; ?></h2> 
<?php 
if(!empty($styles)): // styles are availables for this term 
    foreach($styles as $style): 
     ?> 
     <h3><?php echo $term->name; ?> &gt; <?php echo $style->name; ?></h3> 
     <?php 
     // get the first three products with colour + style 
     $tax_posts = get_posts(array(
      'posts_per_page' => 3, 
      'tax_query' => array(
       'relation' => 'AND', 
       array(
        'taxonomy' => get_query_var('taxonomy'), 
        'terms' => $term->ID 
       ), 
       array(
        'taxonomy' => 'style', 
        'terms' => $style->ID 
       ) 
      ) 
     )); 
     ?> 
     <ul> 
     <?php foreach($tax_posts as $tax_post): ?> 
      <li><?php echo $tax_post->post_title; ?></li> 
     <?php endforeach; ?> 
     </ul> 
     <!-- ... link to your style taxonomy template here ... --> 
    <?pgpp endforeach ; ?> 
<?php else : ?> 
    <!-- ... default behaviour ... --> 
<?php endif; ?> 

你必須通過發送風格長期顏色詞來建立自己的閱讀更多鏈接你的風格分類模板。

+0

這是非常無效的,***爲什麼***你正在運行自定義查詢 –

+0

@PieterGoosen我很開放,看到你對這個問題的答案,但可悲的是我*不*看到任何。 – vard

+0

我會在今晚晚些時候發帖。這是一個惡夢在手機上編碼的東西。 ;-) –

相關問題