2016-11-21 92 views
0

我有一个类别叫'新闻'。类别ID是'20'。我使用Divi(Divi子)主题+ Wordpress,并希望缩短新闻类别的摘录。Wordpress + Divi主题:根据类别控制摘录长度?

我通常会使用 '的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'中找到了摘录控件,并在这里添加我的过滤器?有没有人做过这个?

我加入了“主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> 

它没有打破的主题,但它也没有工作。有没有人有任何这方面的经验?

- 谢谢!

回答

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); 
} 

它可以更容易

希望它可以帮助

+0

最后的解决方案更容易维护。较少的编码。希望他们会改变这个文件是未来更新... – Benoti

0

我最终通过把这个在我的“主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(); 
        }