• Hi
    I have this code for displaying categories and description:
    <?php
    $categories = get_categories(‘exclude=1&title_li=’);
    foreach ($categories as $cat) {
    echo “<h2>category_nicename.”\”>”.$cat->cat_name.”</h2>
    <p>”.$cat->category_description.”(“.$cat->category_count.”)</p>”;
    }
    ?>
    I’m trying now to shorten the description for x amount of characters
    I have this code:
    <?php echo substr( category_description( $cat->cat_ID),0,140 ) . “..”; ?>

    But I cant combine them both – tried some other variations with no success ??

    anyone can help?
    thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Chad

    (@lynneandchad)

    Your code was a bit garbled, so I tried to clean it up a bit. Hopefully this does everything you want:

    $categories = get_categories('exclude=1&title_li=');
    foreach ($categories as $cat) {
      echo "<h2>" . $cat->cat_name . "</h2><p>" . substr( $cat->category_description,0,140 ) . ".." . " (" . $cat->category_count . ")</p>";
    }

    Displays (for each category) the title in an <h2>, followed by the shortened description in a <p> block with the double periods followed by the category count.

    Let me know if that helps! ??

    Thread Starter pikaya

    (@pikaya)

    Hi Chad – in one word = AMAZING!!!
    Thank a lot

    Thread Starter pikaya

    (@pikaya)

    BTW
    how do I add link to each category? Tried but got error ??

    Thread Starter pikaya

    (@pikaya)

    OK solved

    <?php $categories = get_categories(‘exclude=1&title_li=’);
    foreach ($categories as $cat) {
    echo “<h2>category_nicename.”\”>” . $cat->cat_name . “</h2><p>” . substr( $cat->category_description,0,140 ) . “..” . ” (” . $cat->category_count . “)</p>”;
    }?>

    Chad

    (@lynneandchad)

    Glad it’s working. If you wrap your code example in backticks (top left of your keyboard, on the “~” key) it will come out formatted properly ??

    Thread Starter pikaya

    (@pikaya)

    thanks!

    Chad

    (@lynneandchad)

    Any time! If you get a change, please use the dropdown on the right to mark this thread as “resolved” ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘categories and short description’ is closed to new replies.