2017-04-12 12 views
0

私は、「教育」というカスタムの投稿タイプを作っています。このカスタムポストタイプでは、「Years」という名前でカスタム分類を行いました。私はこの形式でこのカスタムポストタイプのおおよその周りの10の記事を追加したと言う:Wordpress:カスタム分類をアルファベット順に表示する

title of custom post type (Education)Custom Taxonomy Name (Year)

どのように私は自分のページ上の記事のタイトルを表示し、それは順番に分類名だのでしょうか?

Vimy College  2014 
Albatross   2013 
Some College  2011 
Another College 2010 
... 
... 

(そのような)

はここで、これまでの私のページのコードです:

<?php /* Template Name: Education Page */ 
get_header(); ?> 

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

<div> 
<?php 
    // The args here might be constructed wrong 
    $args = array('post_type' => 'education', 
        'terms' => 'years', 
        'orderby' => 'terms', 
        'order' => 'ASC'); 

    $loop = new WP_Query($args); 
    while ($loop->have_posts()) : $loop->the_post(); 

     echo '<h3>'; 
     the_title(); 
     echo '</h3>'; 

     // I don't know what else to put here to display the associated taxonomy name 
     // (and in sequential order) 

    endwhile; 
?> 

</div> 

<?php endwhile; endif; ?> 

ので明確にするために、最初のhave_posts()ループだけで実際のページ、および内側のループをエコーポストタイトルを列挙するために上記のカスタム分類法名(この場合は数値)で並べられた形式を生成するはずです。

答えて

1

これは

<?php 
$terms = get_terms('year', array('order' => 'DESC')); 
foreach($terms as $term) { 
    $posts = get_posts(array(
     'post_type' => 'education', 
     'tax_query' => array(
      array(
       'taxonomy' => 'year', 
       'field' => 'slug', 
       'terms' => $term->slug 
      ) 
     ), 
     'numberposts' => -1 
    )); 
    foreach($posts as $post) { 
     the_title(); 
     echo '<br>'; 
     $term_list = wp_get_post_terms($post->ID, 'year', array("fields" => "names")); 
     foreach ($term_list as $t) { 
      echo $t; 
     } 
     echo '<br><br>'; 
    } 
} 
?> 
+0

、素敵な、シンプルでコンパクトなトリックを行う必要があります!ありがとう、これは動作します! – NoReceipt4Panda

1

投稿リストにタクソノミを表示したい場合は、下記のコードを確認してください。

<?php 
$terms = get_terms('years'); 
foreach ($terms as $term) { 
$wpq = array ('taxonomy'=>'years','term'=>$term->slug); 
$myquery = new WP_Query ($wpq); 
$article_count = $myquery->post_count; 
?> 
<?php echo $term->name.':'; 
if ($article_count) { 
while ($myquery->have_posts()) : $myquery->the_post(); 
$feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); 
?> 
<a href="<?php the_permalink(); ?>"> 
<img alt="<?php echo get_the_title(); ?>" src="<?php echo $feat_image;?>"/> 
</a> 
<a href="<?php the_permalink(); ?>"><b><?php echo get_the_title(); ?></b></a> 
<?php endwhile; 
wp_reset_postdata();     
} ?> 

<?php } ?> 
関連する問題