Web Hosting
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

WordPress fix Reply To: Post Category shortcode

Discussion in 'Misc WordPress Requests' started by tilia45, Dec 7, 2017.

  1. tilia45

    Guest

    Reply To: Post Category shortcode, by tilia45

    Hi there,

    You’ll find some helpful information in the function reference:

    https://developer.wordpress.org/reference/functions/get_the_category/

    One important thing to know about get_the_category() is it returns an array of objects. So to display the category names in a post you’ll need to loop through that array. Here’s an example:

    function shortcode_post_category () {
    $html = '';
    $categories = get_the_category();
    foreach( $categories as $category ){
    $html .= '<span>' . $category->name . ' </span>';
    }
    return $html;
    }
    add_shortcode( 'post_category', 'shortcode_post_category' );

    Reply To: Post Category shortcode
     
    #1

Share This Page

Web Hosting