FilT FilT - 23 days ago 7
PHP Question

Post loop for selected category

I'm trying to display all posts from a selected category with thumbnail and title. I've managed to get the code below to show me the selected category, however I can't seem to figure out a loop to display the thumbnail and title for each post of that selected category.

<?php $taxonomy = 'category';
// Get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );

// Separator between links.
$separator = ', ';

if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {

$term_ids = implode( ',' , $post_terms );

$terms = wp_list_categories( array(
'title_li' => '',
'style' => 'none',
'echo' => false,
'taxonomy' => $taxonomy,
'include' => $term_ids
) );

$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );

// Display post categories.
echo $terms;

} ?>


With this code I can get it to show me the thumbnail for the first post in that category:

<a href="<?php get_the_permalink($term_id); ?>">
<?php the_post_thumbnail(array(200, 200)); ?>
</a>


How can I loop this?

Thanks

Answer

Hi if you want to select posts from specific categories try this link. It was kind of helpful for me.