Get all categories title by post id

Get all categories title by post id

In this blog I will show you to get latest 5 posts in wordpress with category names within it, which are assigned to them. WordPress categories are the groups of blogs or posts which are connected to each other.
use below code to get latest 5 posts with category names assign to them.

$args = array(
            'posts_per_page' => -1,
            'post_type' => 'POST_TYPE_HERE',
            'tax_query' => array(
                array(
                    'taxonomy' => 'TAXONOMY_HERE',
                    'field' => 'term_id',
                    'terms' => TEMR_ID_HERE,
                )
            )
        );              

        $loop = new WP_Query($args);
        if($loop->have_posts()) {
            while($loop->have_posts()) : $loop->the_post();
            $src = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_id()), 'thumbnail_size' );
            $imageURL = $src[0];
            ?>
            <a href="<?php the_permlink(); ?>" title="<?php the_title(); ?>">
<img src="<?php echo $url; ?>" alt="<?php the_title(); ?>">
                <h3><?php the_title(); ?></h3>
            </a>
<?php
wp_get_object_terms( $post->ID, 'TAXONOMY_NAME', array( 'fields' => 'names' ) );
            endwhile;
        }

 

Hi, I'm Saurav, the developer behind usingphp. Donate to help me keep usingphp free and maintained.

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Add your comment