Search
 
SCRIPT & CODE EXAMPLE
 

PHP

get_the_category()

#Show the First Category Name Only
  
$categories = get_the_category();
 
if ( ! empty( $categories ) ) {
    echo esc_html( $categories[0]->name );   
}

#Make the first category link to the category page:

$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 response from function:

$categories = get_the_category();
 
var_dump($categories);

array(1) {
[0]=>
  object(stdClass)#310 (17) {
    ["term_id"]=>
    &int(6)
    ["name"]=>
    &string(10) "familylife"
    ["slug"]=>
    &string(10) "familylife"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(6)
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    &string(0) ""
    ["parent"]=>
    &int(0)
    ["count"]=>
    &int(208)
    ["object_id"]=>
    int(7729)
    ["filter"]=>
    string(3) "raw"
    ["cat_ID"]=>
    &int(6)
    ["category_count"]=>
    &int(208)
    ["category_description"]=>
    &string(0) ""
    ["cat_name"]=>
    &string(10) "familylife"
    ["category_nicename"]=>
    &string(10) "familylife"
    ["category_parent"]=>
    &int(0)
  }
}

Comment

PREVIOUS NEXT
Code Example
Php :: laravel hasMany with join 
Php :: readfile in php 
Php :: Warning: sprintf(): Too few arguments in /opt/lampp/htdocs/wordpress/wp-admin/includes/class-bulk-upgrader-skin.php on line 152 
Php :: laravel store array to cache 
Php :: the requested php extension bcmath is missing from your system 
Php :: what is better, php or javascript 
Php :: wp_query post id 
Php :: laravel return view with multiple variable 
Php :: PHP Fatal error: Allowed memory size of 134217728 bytes exhausted 
Php :: Best debugging tools for php 
Php :: laravel Service Unavailable 
Php :: add custom attribute for validation errors laravel 
Php :: hoew to store a cookie php 
Php :: how to log object laravel logger 
Php :: laravel collection distinct 
Php :: concat php 
Php :: carbon get day name 
Php :: laravel validate date 
Php :: learndash edit profile link 
Php :: php loop array 
Php :: remove scientific notation number format in php 
Php :: Mixed Content: The page at was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 
Php :: laravel query order by relation 
Php :: laravel route param blade 
Php :: Laravel How do I get distinct values from the table along with the count of how many rows there are containing that value 
Php :: create model laravel 
Php :: count with condition laravel 
Php :: mail sending setting magneto for mailhog 
Php :: date hour php 
Php :: laravel run all seeders 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =