我有一个名为'新闻'的类别。类别ID为“20”。我正在使用Divi(Divi child)主题+ Wordpress,并希望缩短新闻类别的摘录。
我通常会使用'add_filter'这样的函数:
<pre>
add_filter('excerpt_length', 'news_excerpt_length');
function news_excerpt_length($length) {
if(in_category(20)) {
return 30;
} else {
return 60;
}
}
</pre>
但这不起作用。我在'main-modules.php'中找到了摘录控件,并想在这里添加我的过滤器?有没有人这样做过?
我将'main-module.php'添加到我的子主题的根目录,然后将其添加到我的孩子'functions.php'
<pre>
if ( ! function_exists( 'et_builder_add_main_elements' ) ) :
function et_builder_add_main_elements() {
require ET_BUILDER_DIR . 'main-structure-elements.php';
require 'main-modules.php';
do_action( 'et_builder_ready' );
}
endif;
</pre>
它没有打破主题,但也没有奏效。有没有人对这个特殊问题有任何经验?
-Thanks!
答案 0 :(得分:0)
要覆盖post_excerpt lengh,您可以在custom_functions.php
中找到函数truncate_post()
if ( ! function_exists( 'truncate_post' ) ) {
function truncate_post( $amount, $echo = true, $post = '', $strip_shortcodes = false ) {
global $shortname;
if ( '' == $post ) global $post;
$post_excerpt = '';
$post_excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
if ( 'on' == et_get_option( $shortname . '_use_excerpt' ) && '' != $post_excerpt ) {
if ( $echo ) echo $post_excerpt;
else return $post_excerpt;
} else {
// get the post content
$truncate = $post->post_content;
// remove caption shortcode from the post content
$truncate = preg_replace( '@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate );
// remove post nav shortcode from the post content
$truncate = preg_replace( '@\[et_pb_post_nav[^\]]*?\].*?\[\/et_pb_post_nav]@si', '', $truncate );
// Remove audio shortcode from post content to prevent unwanted audio file on the excerpt
// due to unparsed audio shortcode
$truncate = preg_replace( '@\[audio[^\]]*?\].*?\[\/audio]@si', '', $truncate );
if ( $strip_shortcodes ) {
$truncate = et_strip_shortcodes( $truncate );
} else {
// apply content filters
$truncate = apply_filters( 'the_content', $truncate );
}
// decide if we need to append dots at the end of the string
if ( strlen( $truncate ) <= $amount ) {
$echo_out = '';
} else {
$echo_out = '...';
// $amount = $amount - 3;
}
// trim text to a certain number of characters, also remove spaces from the end of a string ( space counts as a character )
$truncate = rtrim( et_wp_trim_words( $truncate, $amount, '' ) );
// remove the last word to make sure we display all words correctly
if ( '' != $echo_out ) {
$new_words_array = (array) explode( ' ', $truncate );
array_pop( $new_words_array );
$truncate = implode( ' ', $new_words_array );
// append dots to the end of the string
$truncate .= $echo_out;
}
if ( $echo ) echo $truncate;
else return $truncate;
};
}
}
您不需要放置if ( ! function_exists( 'truncate_post' ) ) {
使其工作只需在functions.php中创建具有相同名称的函数
如果您正在使用子主题(我真的希望这样的主题),请复制/粘贴index.php并粘贴这些行,第54行
if(in_category(20)) {
truncate_post( 30 );
} else {
truncate_post( 60 );
}
可以更容易
希望有所帮助
答案 1 :(得分:0)
我最终做到了(虽然我不知道哪种解决方案更好)将它放在我的'main-module.php'中,从第12319行开始
// do not display the content if it contains Blog, Post Slider, Fullwidth Post Slider, or Portfolio modules to avoid infinite loops if ( ! has_shortcode( $post_content, 'et_pb_blog' ) && ! has_shortcode( $post_content, 'et_pb_portfolio' ) && ! has_shortcode( $post_content, 'et_pb_post_slider' ) && ! has_shortcode( $post_content, 'et_pb_fullwidth_post_slider' ) ) { if ( 'on' === $show_content ) { global $more; // page builder doesn't support more tag, so display the_content() in case of post made with page builder if ( et_pb_is_pagebuilder_used( get_the_ID() ) ) { $more = 1; the_content(); } else { $more = null; the_content( esc_html__( 'read more...', 'et_builder' ) ); } } else { if ( has_excerpt() ) { the_excerpt(); } else { if(in_category(20)) { echo wpautop( truncate_post( 70, false ) ); } else { echo wpautop( truncate_post( 370, false ) ); } } } } else if ( has_excerpt() ) { the_excerpt(); }