【问题标题】:Wordpress + Divi theme: control excerpt length based on category?Wordpress + Divi 主题:根据类别控制摘录长度?
【发布时间】:2017-04-05 04:10:27
【问题描述】:

我有一个名为“新闻”的类别。类别 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>

它没有破坏主题,但它也不起作用。有人对这个特定问题有任何经验吗?

-谢谢!

【问题讨论】:

    标签: wordpress categories


    【解决方案1】:

    要覆盖 post_excerpt 长度,您可以在 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 );
    }
    

    这样会更容易

    希望对你有帮助

    【讨论】:

    • 最后一个解决方案更容易维护。更少的编码。希望他们会在未来的更新中更改此文件...
    【解决方案2】:

    我最终做到了(虽然我不知道哪个解决方案更好),把它放在我的“main-module.php”中,从第 12319 行开始

    // 如果内容包含 Blog、Post Slider、Fullwidth Post Slider 或 Portfolio 模块,则不显示内容以避免无限循环 如果(!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) { 全球$更多; // 页面构建器不支持更多标签,因此在使用页面构建器发布帖子的情况下显示 the_content() 如果(et_pb_is_pagebuilder_used(get_the_ID())){ $更多 = 1; 内容(); } 别的 { $更多=空; the_content( esc_html__( '阅读更多...', 'et_builder' ) ); } } 别的 { if ( has_excerpt() ) { the_excerpt(); } 别的 { 如果(in_category(20)){ echo wpautop(truncate_post(70, false)); } 别的 { 回声 wpautop(truncate_post(370, false)); } } } } 否则 if ( has_excerpt() ) { the_excerpt(); }

    【讨论】:

      猜你喜欢
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2014-04-12
      • 2015-05-19
      • 1970-01-01
      相关资源
      最近更新 更多