显示自定义帖子类别及其帖子

时间:2013-05-24 06:26:24

标签: wordpress

我正在尝试从自定义帖子类型中显示类别标题及其帖子。我可以显示要显示的类别,但它目前列出了自定义帖子区域中的所有帖子,而不是将它们分隔到

<?php 
$taxonomy = 'staff';
$cat_args = array(
'taxonomy' => $taxonomy,
'tax_input' =>$tax_input,
  'orderby' => 'name',
  'order' => 'ASC',
  'child_of' => 0
);

$tax_terms = get_terms($taxonomy); 

foreach ($tax_terms as $tax_term) { 
    echo '<div class="categorybox">';
    echo '<h4> <a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></h4>';

     $args = array(
     'post_type' => 'staff',
     "singular_label" => "Department",
      'numberposts' => 5,
      'taxonomy' => $taxonomy->$tax_term,
    );

    $posts = get_posts($args);
    ?>
    <ul><?php
    foreach($posts as $post) {
    ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php 
    } ?>
    </ul><?php
    echo '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>View all articles in ' . $tax_term->name.' &raquo;</a>';
    echo '</div>';
} 
?>

2 个答案:

答案 0 :(得分:0)

taxonomy中没有选项get_posts()。试试这个

'tax_query' => array(
        array(
            'taxonomy' => 'staff',
            'field' => 'slug',
            'terms' => $tax_term->slug
        )
    )

答案 1 :(得分:0)

<?php
    $query = new WP_Query( array( 'post_type' => 'testimonials','posts_per_page' => 10 ) ); //testimonials custom post type
    while($query->have_posts()) : $query->the_post();
?>
<ul>
    <li>
        Category:<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_category(', '); ?></a> 
        <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    </li>
</ul>
<?php endwhile; ?>