Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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)
  }
}

Source by developer.wordpress.org #
 
PREVIOUS NEXT
Tagged:
ADD COMMENT
Topic
Name
4+6 =