Get all categories title by post id in WordPress

Get all categories title by post id in WordPress

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;
        }

 

Also Read: Get posts by meta key and meta value in WordPress
Also Read: Get all categories from custom post type in WordPress

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;
        }

 

Also Read: Get posts by meta key and meta value in WordPress
Also Read: Get all categories from custom post type in WordPress

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

Close