2015-11-22 83 views
2

我創建了自定義帖子和分類。現在我想要這樣做。獲取所有類別,然後顯示每個術語中的所有帖子

    分類學名稱1
  • 後1
  • 柱2
  • 柱3
  • 交所有
    分類學名稱2
  • 後1
  • 後2
  • 柱3
  • 交所有
    分類學名稱4
  • 後1
  • 柱2
  • 柱3
  • 交所有

並繼續如此。 我發現了一個代碼,但這不起作用。

代碼

<?php 
$taxonomy = 'category'; 
$param_type = 'category__in'; 
$term_args=array(
    'orderby' => 'name', 
    'order' => 'ASC'); 
$terms = get_terms($taxonomy,$term_args); 
if ($terms) { 
    foreach($terms as $term) { 
     $args=array(
      "$param_type" => array($term->term_id), 
      'post_type' => 'post', 
      'post_status' => 'publish', 
      'posts_per_page' => -1, 
      'caller_get_posts'=> 1 
     ); 
     $my_query = null; 
     $my_query = new WP_Query($args);  
     if($my_query->have_posts()) { ?> 
      <div class="category section"> 
      <h3><?php echo 'Category '.$term->name;?></h3> 
      <ul><?php while ($my_query->have_posts()) : $my_query->the_post(); ?> 
         <li><a href="<?php the_permalink() ?>" rel="bookmark" 
    title="Permanent Link to <?php the_title_attribute(); ?>"> 
         <?php the_title(); ?></a></li> 
        <?php endwhile; ?> 
      </ul> 
     </div> 
    <?php} 
}} 

wp_reset_query(); // Restore global post data stomped by the_post().?> 

所以請幫助我。如何解決它。 謝謝

回答

3

我假設你試圖從分類標準Category中獲取任何帖子。

<?php 
$cat_terms = get_terms(
       array('category'), 
       array(
         'hide_empty' => false, 
         'orderby'  => 'name', 
         'order'   => 'ASC', 
         'number'  => 6 //specify yours 
        ) 
      ); 

if($cat_terms) : 

    foreach($cat_terms as $term) : 

     //var_dump($term); 
     echo '<h3>'. $term->name .'</h3>'; 

     $args = array(
       'post_type'    => 'post', 
       'posts_per_page'  => 10 //specify yours 
       'post_status'   => 'publish', 
       'tax_query'    => array(
              array(
               'taxonomy' => 'category', 
               'field' => 'slug', 
               'terms' => $term->slug, 
              ), 
             ), 
       'ignore_sticky_posts' => true //caller_get_posts is deprecated since 3.1 
      ); 
     $_posts = new WP_Query($args); 

     if($_posts->have_posts()) : 
      while($_posts->have_posts()) : $_posts->the_post(); 

       echo '<h3>'. get_the_title() .'</h3>'; 

      endwhile; 
     endif; 
     wp_reset_postdata(); //important 


    endforeach; 

endif; 

請注意caller_get_posts參數從3.1開始已棄用。請務必諮詢Codex以獲取最新的代碼指導,並且不要使用棄用的代碼。

WP_Query() - WordPress的法典

0

你可以試試這個,你可能想把它移到您的模板中而不是在函數內部它,確保$tax被正確分類,如果你還沒有使用改變post_type原生wordpress Post

function get_taxonomy_and_post() { 
    $tax = 'category'; // Your Taxonomy, change it if you not using wordpress native category 
    $terms = get_terms($tax ,array(// get all taxonomy terms 
     'orderby' => 'name', 
     'order'  => 'ASC', 
     'hide_empty' => 0, 
    )); 

    //Loop throug each taxonomy terms, 
    foreach ($terms as $term) { 

     //Query argument for post 
     $args = array( 
      'post_type' => 'post', // Or Custom Post Type, 
      'order' => 'DESC', 
      'orderby' => 'date', 
      'taxonomy' => $tax, 
      'term' => $term->slug, // Query posts for each term based on term slug 
     ); 
     $query = new WP_Query($args); 
     $posts = $query->get_posts(); 
     echo '<div class="category section"><h3>Category '.$term->name.'</h3><ul>'; 
     if ($posts) { 
      foreach ($posts as $post) { 
       echo '<li><a href="'.$post->guid /**use get_permalink($post->ID) if you want the custom permalink**/.'">'.$post->post_title.'</a><li>'; 
      } 
     } 
     echo '</ul></div>'; 

    } 
} 
add_action('wp_head', 'get_taxonomy_and_post'); 
相關問題