代码之家  ›  专栏  ›  技术社区  ›  Duc Phuli

如何为产品说明添加“读取”按钮?

  •  0
  • Duc Phuli  · 技术社区  · 7 年前

    只需创建一个新函数来处理get_the_content()的值,以仅获取最大字数,并在末尾添加一个“Read more”链接:

    function custom_single_product_summary(){
      $maxWords = 50; // Change this to your preferences
      $description = strip_tags(get_the_content()); // Remove HTML to get the plain text
      $words = explode(' ', $description);
      $trimmedWords = array_slice($words, 0, $maxWords);
      $trimmedText = join(' ', $trimmedWords);
    
      if(strlen($trimmedText) < strlen($description)){
        $trimmedText .= ' &mdash; <a href="' . get_permalink() . '">Read More</a>';
      }
    
      echo $trimmedText;
    }
    

    好像没用 有人能帮我谢谢

    1 回复  |  直到 7 年前
        1
  •  0
  •   Sunil Dora    7 年前

    请检查一下这个,

    <?php the_content( $more_link_text , $strip_teaser ); ?> 
    

    我在这里找到的 Customizing the Read More

    希望这对你有帮助。