Posts list with their taxonomy

93 views Asked by At

I would like to display list of post for a wordpress post type but I don't know how to show taxonomy term linked to the post. My code :

<ul class="list-group"> 
    <?php
    $args = array(
        'post_type' => 'faq',
        'post_status' => 'publish',
        'orderby'          => 'date',
        'order'            => 'DESC',
        'posts_per_page' => 5,

    )
    ;$myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
        <li class="list-group-item">
        <span class="label label-default">xxxxx taxonomy xxxxxx</span>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
    <?php endforeach; 
    wp_reset_postdata();?>
    </ul>

Could you help me please.

2

There are 2 answers

0
Manish On
<?php
/* FIRST
 * Note: This function only returns results from the default “category” taxonomy. For custom taxonomies use get_the_terms().
 */
$categories = get_the_terms( $post->ID, 'taxonomy' );
// now you can view your category in array:
// using var_dump( $categories );
// or you can take all with foreach:
foreach( $categories as $category ) {
    echo $category->term_id . ', ' . $category->slug . ', ' . $category->name . '<br />';
}
1
Manish On
<?php query_posts(array('post_type'=>'faq', 'posts_per_page'=>5, 'order' => 'DESC', )); ?>
    <?php if(have_posts()) { while(have_posts()) {  the_post();?>   
    <div class="col-md-6 col-sm-6">
        <?php if ( has_post_thumbnail() ) {?>
            <div class="blog_image">
                <?php the_post_thumbnail( 'full' ); ?>
            </div>
        <?php } else{ ?>
        <?php }?>
        <div class="entry-content">
            <h3 class="blog-title">
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </h3>
            <span class="post-date"><?php echo get_the_date('d. M Y'); ?></span>
            <p><?php echo wp_trim_words( get_the_content(), 50, ' (...)' );?></p>
        </div>
    </div>
<?php }
    }
    wp_reset_query(); 
?>

 for image display <?php the_post_thumbnail( 'full' ); ?> 
 for title display <?php the_title_attribute(); ?> for title display 
 for content display <?php the_content();?> 

change HTML as you want