0
votes

I've got post_type = portfolio in my wordpress page, and with this code I get the category of each post:

$terms = get_the_terms( $post->ID, 'portfolio_category' );

foreach ( $terms as $term ) {
    $draught_links[] = $term->name;
}

$on_draught = join( ", ", $draught_links );

But it's not working correctly. It shows the category of current post, and of all posts before. How to fix it?

1
A little more context might be needed to answer this question. For instance, is this code within "The Loop" or outside of it? - Chaser324
and if it is within a loop consider initialising $draught_links =array() just before foreach ( $terms as $term ) - Mwayi
@user622018 That seems like a pretty likely cause. - Chaser324
user622018 it's working. Thanks ;) - Daniel Koczuła

1 Answers

3
votes

use this code for specific category

<?php
global $post;
$args = array( 'numberposts' => 5, 'category' => 3 );


$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>

<?php the_title(); ?>
<?php the_content(); ?>

Chnage the number of post and category ID....