admin管理员组文章数量:1125590
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>';
?>
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>';
?>
Share
Improve this question
edited Sep 12, 2015 at 8:30
tali
asked Sep 12, 2015 at 3:48
talitali
151 silver badge8 bronze badges
5
|
3 Answers
Reset to default 3I'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.
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")
}
});
');
}
Late to the game but this is the hook for anyone using WC 6.7.0 or higher.
woocommerce_taxonomy_archive_description_raw
本文标签: filtersHow to customize categorydescription()
版权声明:本文标题:filters - How to customize category_description()? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736660668a1946399.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$Ecategory->description
in your code should becategory_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 Commented Sep 12, 2015 at 9:25category_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