display category name wordpress code example

Example 1: wordpress get post category name

<?php
$categories = get_the_category();
 
if ( ! empty( $categories ) ) {
    echo esc_html( $categories[0]->name ); // Show first item of category  
}
?>

Example 2: display category name wordpress

<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
	echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
}
?>

Example 3: how to get display name in wordpress

<?php 
	$user_info = get_userdata(1);
	echo 'Username: ' . $user_info->user_login . "\n";
	echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
	echo 'User ID: ' . $user_info->ID . "\n";
?>

Tags:

Misc Example