• I’m trying to follow this article as best I can:
    // https://www.wpbeginner.com/wp-tutorials/how-to-display-category-descriptions-in-wordpress/

    However, I need to do everything from the wp-cli so I can’t follow the directions precisely but I did my best. In particular, I added the below code to a “storefront” child theme’s functions.php file I created for this purpose (although just this minute I learned about “site specific plugins” and I may decide to do it that way instead which I guess is beside the point.)

    Unfortunately, short codes display the raw short code text rather than executing the function. I suspect I’m missing something like some kind of hook tie-in or something, but I don’t know what exactly. Some of the pages I’ve read hint strongly at this, but I was unable to translate to my specific situation. Thank you in advance!

    function wpb_catlist_desc() {
    $string = '<ul>';
    $catlist = get_terms( 'category' );
    if ( ! empty(  ) ) {
      foreach ( $catlist as $key => $item ) {
        $string .= '<li>'. $item->name . '<br />';
        $string .= '<em>'. $item->description . '</em> </li>';
      }
    }
    $string .= '</ul>';
    
    return $string;
    }
    
    add_shortcode('wpb_categories', 'wpb_catlist_desc');

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    if ( ! empty( ) ) {

    that’s not valid. empty() requires a paraemeter

    Hello apb1963,
    Below given is the correct code you should use that:-

    function wpb_catlist_desc() {
    $sring = '<ul>';
    $catlist = get_terms( 'category' );
    if ( ! empty( $catlist ) ) {
      foreach ( $catlist as $key => $item ) {
        $string .= '<li>'. $item->name . '<br />';
        $string .= ',em>'. $item->description . '</em> </li>';
      }
    }
    $string .= '</ul>';
     
    return $string;
    }
    add_shortcode('wpb_categories', 'wpb_catlist_desc');

    Then if you want to display it on any pages or post then use this [wpb_categories] or else use this echo do_shortcode(‘[wpb_categories]’);

    Thread Starter apb1963

    (@apb1963)

    Thanks for responding, but as far as I can tell your code is identical to mine with the exception that you have a typo in your string, namely

    ,em>'
    
    $string .= ',em>'. $item->description . '</em> </li>';

    The missing parameter for the empty() function was due to a missing backslash in my bash script. I already fixed that minor issue but I get the same results. Something else is wrong.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘shortcodes display raw shortcode text’ is closed to new replies.