2015-12-02 169 views
0

這個問題似乎已經在某些品種中提出過,但我的具體情況很明確。如何在wordpress中顯示自定義帖子類型的每個類別的最新帖子

我需要顯示稱爲「產品」的自定義帖子類型的所有類別的最新帖子。我需要顯示類別而不是標題,並鏈接到該檔案的檔案。

問題是,我所得到的只是該普通帖子類型的一個帖子。有什麼想法嗎?

這裏是我的出發點:

<?php 

     $query = new WP_Query(array(
     'posts_per_page' => 12, 
     'post_type' => 'products', 
     'post_status'=>'publish', 
     'orderby'=>'post_date', 
     'order' => 'DESC', 
     'cat' => $category->term_id, 
     'count'=>1, 

     )); 

     if (have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> 

     <div class="col-sm-6 col-md-4"> 

       <?php if (has_post_thumbnail()) : ?> 

       <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="pos-rel"> <?php the_post_thumbnail('product', array('class' => 'img-responsive center-block')); ?><h3><?php the_title(); ?></h3></a> 

       <?php else : ?> 

       <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="featured-products pos-rel"><!--<img class="img-responsive center-block" src="<?php echo THEME_DIR; ?>/img/default-thumb.png" alt="<?php the_title(); ?>" />--><img class="img-responsive " data-src="holder.js/100%174/auto" alt="Generic placeholder image"><h3><?php the_title(); ?></h3></a> 

       <?php endif; ?> 

     </div><!-- col-sm-6 --> 

     <?php endwhile; ?> 
     <?php wp_reset_postdata(); ?> 
     </div><!-- end row --> 
     <ul class="pager"> 
      <li class="previous"><?php previous_posts_link(); ?></li> 
      <li class="next"><?php next_posts_link(); ?></li> 
     </ul> 

     <?php else : ?> 
     <h1>Not Found</h1> 
     <p><a href="<?php echo home_url(); ?>" title="Home">Click Here to return to the Home Page</a></p> 
     <?php endif; ?> 

的自定義後類型是通過第三插頭創建成Super CPT插件

這裏是代碼:

add_action('after_setup_theme', 'setup_data_structures'); 
function setup_data_structures() { 
if (! class_exists('Super_Custom_Post_Type')) 
return;

$products = new Super_Custom_Post_Type('products', 'Product', 'Products');

# Test Icon. Should be a square grid. $products->set_icon('suitcase'); # Taxonomy test, should be like categories $product_category = new Super_Custom_Taxonomy( 'product-category', 'Product Category', 'Product Categories', 'category'); # Connect both of the above taxonomies with the post type connect_types_and_taxes($products, array($product_category)); # Add a meta box with every field type $products->add_meta_box(array( 'id' => 'product-fields', 'context' => 'normal', 'fields' => array( 'client-name' => array('column' => true), 'product-description' => array('type' => 'wysiwyg') ) ));

$產品 - > add_to_columns('product-category');

+0

我不知道有關該插件,因爲我通過職能文件創建自己的插件。 add_action('init','create_post_type'); 功能create_post_type(){ register_post_type( '產品', 陣列( '標籤'=>數組( '名稱'=> __( '產品'), 'singular_name'=> __( '產品') ), 'public'=> true, 'has_archive'=> true, ) ); } –

+0

這就是我會做的。但是我有一個以前的開發人員在這裏工作。 – psyxx

回答

0

這應該會幫助你。以下代碼顯示每個類別的5個。只需將5改爲您希望顯示的每個類別的數量。

<?php 
    $cat_args = array(
    'orderby' => 'name', 
    'post_type' => 'products', 
    'order' => 'ASC', 
    'child_of' => 0 
    ); 

$categories = get_categories($cat_args); 

foreach($categories as $category) { 
    echo '<dl>'; 
    echo '<dt> <a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name.'</a></dt>'; 

    $post_args = array(
     'numberposts' => 5, 
     'category' => $category->term_id 
    ); 

    $posts = get_posts($post_args); 

    foreach($posts as $post) { 
    ?> 
     <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd> 
    <?php 
    } 
    echo '<dd class="view-all"> <a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>View all posts in ' . $category->name.'</a></dd>'; 
    echo '</dl>'; 
    } 
    ?> 
+0

正確。將帖子類型添加到$ cat_args 'post_type'=>'產品' –

+0

謝謝。仍生成帖子的結果,而不是自定義帖子類型。 – psyxx

0
<?php 
    $cats = array('3d-mail', 'boxes', 'folders', 'binders'); 
    $exclude_posts = array(); 
    foreach($cats as $cat) 
    { 
     // build query argument 
     $query_args = array(
      'category_name' => $cat, 
      'showposts' => 1, 
      'post_type' => 'products', 
      'post_status' => 'publish', 
      'orderby' => 'date', 
      'order' => 'DESC' 
     ); 

     // exclude post that already have been fetched 
     // this would be useful if multiple category is assigned for same post 
     if(!empty($exclude_posts)) 
      $query_args['post__not_in'] = $exclude_posts; 

     // do query 
     $query = new WP_Query($query_args); 

     // check if query have any post 
     if ($query->have_posts()) { 

      // start loop 
      while ($query->have_posts()) { 
       // set post global 
       $query->the_post(); 

       // add current post id to exclusion array 
       $exclude_posts[] = get_the_ID(); 


       <?php the_content(); ?> 
      } 
     } else { 
      // no posts found 
     } 

     // Restore original Post Data 
     wp_reset_postdata(); 
    } 

?> 
+0

謝謝。但是這列出了主要類別。不屬於我的自定義帖子類型。 – psyxx

+0

正確。將帖子類型添加到$ cat_args'post_type'=>'產品' –

+0

我已更新代碼以反映您的自定義帖子類型。 –

相關問題