0

In a website I am working on ,there are 50 categories + sub categories, each has a a very long description text, then in an archive page I display all the titles of the categories + their description ,but it makes more sense to show the title of each category + a few words of each description (excerpt?) and a "read more" link ,to the full content of that category .

so I have read the codex and also a lot of articles on the Web, but still I cant understand the reason why this sort of functionality doesn't exist in WordPress by default. It exists only for posts -Function Reference/the excerpt.

Second question is what is the most elegant way to solve it ?

There are a few plugins or snippets of code that enable you to add html editor to the category description in the admin side, but selecting the read more link doesnt work there:(

Is the only solution to write a very long Hook/filter on the category_description() function ?

to elaborate: this is my code in category.php , - what is does is diplaying the parent category and all its children(titles+ full descriptions)

    <?php 
    $CategoryPar = get_category( get_query_var( 'cat' ) ); 
    $cat_id = $CategoryPar->cat_ID;
      $args = array(
      'orderby' => 'name',
      'child_of'           => $cat_id,
      'hide_empty' => FALSE,
      'order' => 'ASC'
            );
    $Ecategories = get_categories($args);

    echo'<div class="cat-sub-title">';

    foreach($Ecategories as $Ecategory) { 
     echo '<p><a href="' . get_category_link( $Ecategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $Ecategory->name ) . '" ' . '>' . $Ecategory->name.'</a> </p> ';
     echo '<div class="cat-sub-title-desc">'. $Ecategory->description . '</div>';
                       } 
    echo'</div>';

    ?>
  • Is the only solution to write a very long Hook/filter on the category_description() function. What do you mean. What else are you looking for – Pieter Goosen Sep 12 '15 at 8:57
  • Hi Pieter ,first I am trying to understand the logic , if category is a taxonomy , why can't it have a description and a short description just like a post , why it is not default in wordpress? – tali Sep 12 '15 at 9:14
  • $Ecategory->description in your code should be category_description($Ecategory->term_id ), then @cybmeta solution works. As to why there are not separate functions, I don't know, but my guess is is that it is really not necessary, therefor the filter for the 0.01% that needs to change the description ;-) – Pieter Goosen Sep 12 '15 at 9:25
  • I think that the question is not why WordPress has not auto excerpts functionality for categories description, the proper question is why you neet a (quoting you) "very long category description"; very long category description has little/no sense from my point of view. If you have a "cars" cateogry, the description is not for explaning what a car is but to explain that you are going to find posts that belongs to car's category. Anyway, you asked how to customize the output of the category_description() function and you have a working and tested answer. Not sure what more to say to help you. – cybmeta Sep 12 '15 at 10:27
  • thank you all :)I will try in a minute to change things and test them in the code . for the logic , well of course this is the request of my client. his website is very informative website (sorry for my english hope I say it correctly) not ment to sell anything. is it not what wordpress was ment to be(words/text)? ...and for the cars example , well if you want to sell cars in your website ,then of course you want to get to a specific car post as fast as you can,but what if its a site like wikipedia, you can find long description on each taxonomy like birds flowers and cars in general ... – tali Sep 12 '15 at 11:31
2

I've been looking for a filter for category_description() and I have not found any. You could use wp_trim_words() with category_description() to get the desired result. For example:

$cat_ID = 4;

// wp_trim_words( $text, $num_words = 55, $more = null );
echo wp_trim_words( category_description( $cat_ID ), 55, '<a href="' . get_category_link( $cat_ID ) . '">' . __("Read more", "text-domain" ) . '</a>' );

Update: I've found the filter

add_filter( 'category_description', 'cyb_trim_category_desc', 10, 2 );
function cyb_trim_category_desc( $desc, $cat_id ) {

    // wp_trim_words( $text, $num_words = 55, $more = null );
    $desc = wp_trim_words( $desc, 55, '<a href="' . get_category_link( $cat_id ) . '">' . __("Read more", "text-domain" ) . '</a>' );

    return $desc;
}

Note: if you use the generic the_archive_description() function in your theme, the above filter works perfectly for categories archvie.

  • I have added some code above, but the trim works only on the first category (parent category) – tali Sep 12 '15 at 8:31
  • In the code you have added to the question you are using $Ecategory->description, not category_description() function (you asked specifically about this function), so the category_description filter won't be applied. Note that you are printing the category description field directly from the database results. As @PieterGoosen said, you need to replace $Ecategory->description with category_description($Ecategory->term_id) or apply wp_trim_words() directly to $Ecategory->description. – cybmeta Sep 12 '15 at 10:19
  • yesssssssssssssss its working :))) wow what a relief!!! thank you so much cybmeta ,thank you so much pieter :)) now I will try to understand why you choose to use category_description($Ecategory->term_id) instead of this $Ecategory->description.thanks again – tali Sep 12 '15 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.