2016-11-21 71 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(); 
        }