I have some code that loops through all categories and:
organize them on a Hierarchy of (parent) category and its child categories
echo the titles +short description of each category +add "read more" link to the full content of that category .
click on the read more of one of the child category should have lead to that category page with full content description
checks if they have posts or not.
displays a list of posts (excerpt+read more link) that belong to that category
Now when a user is clicking on one of the child categories (read more link) it leads to another page that shows only that category , with or without posts , but I want it to show now the full description of that category not just the short description ..
I thought if I had a way to add a unique class and by that target the pages with one category (child category) , I can solve this/is this the right way?
The code in category.php:
<?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>';
?>
<?php if( category_has_children( $cat ) == false) : ?>
<?php get_template_part( 'loop' );?>
<?php endif; ?>
and the filter in functions.php
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, '...<p class="wrap-more-link"><a class="more-link" href="' . get_category_link( $cat_id ) . '">' . __("Read more >>", "text-domain" ) . '</a></p>' );
return $desc;
}
First image - click on one one of the categories in the main menu will lead the user to this page (its not a template page)
second image - clicking on one of the read more links of the child categories in the previous page will lead to this page - that child category has no posts ,I want it to show full description length .these are the body classes
class="archive category category-eternal-in-the-field category-23"