2017-04-12 168 views
0

我制作了一个名为“Education”的自定义帖子类型。使用此自定义帖子类型,我通过“年份”的名称进行了自定义分类。说我是添加了这种自定义后类型大致在10个职位格式为:WordPress的:显示自定义帖子类型Alphabetized自定义分类?

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

如果你想显示你的taxonomy与你的帖子列表请检查下面的代码。

<?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 } ?> 
相关问题