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>';

    ?>
5
  • 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 Commented Sep 12, 2015 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
    Commented Sep 12, 2015 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 ;-) Commented Sep 12, 2015 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
    Commented Sep 12, 2015 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
    Commented Sep 12, 2015 at 11:31

3 Answers 3

3

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.

3
  • I have added some code above, but the trim works only on the first category (parent category)
    – tali
    Commented Sep 12, 2015 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
    Commented Sep 12, 2015 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
    Commented Sep 12, 2015 at 11:42
1

Here is the solution that worked on my end using jquery pasted this code on function.php file. this is only if you want to truncate the description of your category page on woocommerce.

/**
 * @snippet       Truncate Short Description @ WooCommerce category description
 * @author        Steve Ayo
 */
 

 
add_action( 'woocommerce_after_main_content', 'bbloomer_woocommerce_short_description_truncate_read_more' );
 
function bbloomer_woocommerce_short_description_truncate_read_more() { 
   wc_enqueue_js('
        var show_char = 300;
        var ellipses = "... ";
        var content = $(".term-description").html();
        if (content.length > show_char) {
            var a = content.substr(0, show_char);
            var b = content.substr(show_char - content.length);
            var html = a + "<span class=\'truncated\' style=\'display:none\'>" + b + "</span> <span class=\'truncated-expander\'>" + ellipses + "<a href=\'#\' class=\'read-more\'>Read more</a></span>";
            $(".term-description").html(html);
        }
        $(".read-more").click(function(e) {
            e.preventDefault();
            $(".term-description .truncated").toggle();
            if($(".term-description .truncated").is(":visible")){
                $(".read-more").text("Show Less")
            } else{
                $(".read-more").text("Read More")
            }
        });

   ');
}
0

Late to the game but this is the hook for anyone using WC 6.7.0 or higher.

woocommerce_taxonomy_archive_description_raw
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 8 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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