If you want to get posts from WordPress through Category Id, there are many ways to do so. Visit the WordPress tutorial page to learn more. But here we will show you how easy one by following the example code below:

<div class="container">
<?php
$categories = get_the_category();
$category_id = $categories[0]->term_id;
echo '<h2 class="pb-2 border-bottom">'.$categories[0]->name.'</h2>';
echo '<div class="row row-cols-1 row-cols-lg-1 align-items-stretch py-5">';
$last_posts = new WP_Query( array(
    'post_type'           => 'post',
    'category__in'  => $category_id,
    'post_status'  => 'publish',
    'posts_per_page'      => 12,
    //'orderby'             => 'name',
    'order'               => 'desc'
) );
if($last_posts->have_posts()):?>
<div class="list-group w-auto">
<?php
while ($last_posts->have_posts()):
    $last_posts->the_post();
     if (has_post_thumbnail()):
        $post_thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $last_posts->ID ),'full');
     else:
        $post_thumbnail = array(suostei_theme_uri.'/assets/noimage.jpg','');
     endif;
    ?>
        <div class="d-flex gap-2 w-100 justify-content-between mb-3">
            <div>
              <h4 class="mb-3 text-primary"><a class="link-info" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
              
              <p class="mb-2 text-small"><?php echo gen_string_tiny(suostei_excerpt());?></p>
              <small class="text-nowrap">
                  <time datetime="<?php echo get_the_date('c'); ?>" itemprop="datePublished"><?php echo get_the_date($last_posts->ID); ?></time>
                  <!--<time datetime="<?php echo get_the_date('c'); ?>" itemprop="datePublished"><?php echo get_the_date(); ?></time>--> 
                  <span class="float-end">
                      
                      <?php 
                        $categories = get_the_category($last_posts->ID);
                        if ( ! empty( $categories ) ) {
                           echo '<a href="'.esc_attr(esc_url(get_category_link($categories[0]->term_id))).'">'.esc_html( $categories[0]->name ).'</a>';		
                        }
                      ?>

                  </span>
              </small>
            </div>
        </div>
    <hr/>
    <?php 
    endwhile;
    ?>
</div>
    <div class="col-lg-12 mb-1 mt-3 pt-3">
       <?php do_action('post_pagination');?>
    </div>
    <?php
    endif;
    echo '</div>';
    ?>
</div>


Please notice that, you need to change some variable that are using here by replace with your desire. The best part you need to see is look like below:

<?php
$categories = get_the_category();
$category_id = $categories[0]->term_id;
$last_posts = new WP_Query( array(
    'post_type'           => 'post',
    'category__in'  => $category_id,
    'post_status'  => 'publish',
    'posts_per_page'      => 12,
    //'orderby'             => 'name',
    'order'               => 'desc'
) );
?>

That’s it! this code is working fine for wordpress 4.5 and above.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *