{"id":28810,"date":"2016-12-27T21:01:13","date_gmt":"2016-12-27T13:01:13","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=28810"},"modified":"2017-11-23T00:13:09","modified_gmt":"2017-11-22T16:13:09","slug":"shave-plugin","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/","title":{"rendered":"Dynamic Truncated Text with Shave.js Plugin"},"content":{"rendered":"<p>Most WordPress blogs use the \"read more\" feature to show preview text from a post. This text is truncated and cut off at a certain point to save space and to <strong>encourage readers to click further to keep reading<\/strong>.<\/p>\n<p>But sometimes you\u2019ll want to add this feature onto a single page. Enter <a target=\"_blank\" href=\"https:\/\/github.com\/dollarshaveclub\/shave\">Shave.js<\/a>, the JavaScript plugin made for <strong>dynamically truncating content<\/strong>.<\/p>\n<figure><a target=\"_blank\" href=\"https:\/\/dollarshaveclub.github.io\/shave\/\"><img decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/shave-plugin\/01-shave-plugin-homepage.jpg\" alt=\"shave plugin homepage\"><\/a><\/figure>\n<p class=\"recommended_top\">\n\t\t\t\t\t<strong>Read Also:<\/strong>\u00a0\n\t\t\t\t\t<a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/rich-snippets-schema-org-vocab\/\">Boost Your Search Results with Schema.org and Rich Snippets<\/a>\n\t\t\t\t<\/p>\n<p>A cool fact about this plugin is that it\u2019s created by <a target=\"_blank\" href=\"https:\/\/www.dollarshaveclub.com\/\">Dollar Shave Club<\/a>, the team who made one of the <a target=\"_blank\" href=\"https:\/\/www.youtube.com\/watch?v=ZUG9qYTJMsI\" rel=\"nofollow\">funniest ads<\/a> I\u2019ve ever seen. I wasn\u2019t aware their team even had a GitHub page but it\u2019s <a target=\"_blank\" href=\"https:\/\/github.com\/dollarshaveclub\">full of repos<\/a> both original and forked.<\/p>\n<p>This particular plugin is fairly new and it already has 800+ stars. It\u2019s dependency-free so it <strong>can run on plain JavaScript regardless of the browser or other included libraries<\/strong>.<\/p>\n<p>Code setup is also pretty simple with the <strong>shave()<\/strong> function only taking two parameters: an <strong>element selector<\/strong> and a <strong>max height<\/strong> for that element. Here\u2019s a very basic example:<\/p>\n<pre>\r\n    maxheight = 320;\r\n    shave('.elemclass', maxheight);\r\n<\/pre>\n<p>Naturally there are <strong>extra parameters you can pass for custom characters<\/strong> after the truncated text, or <strong>multiple selectors where you want to apply the shave effect<\/strong>.<\/p>\n<p>You can actually see a live demo on the <a target=\"_blank\" href=\"https:\/\/dollarshaveclub.github.io\/shave\/\">Shave plugin site<\/a> and they have a nice <a target=\"_blank\" href=\"https:\/\/codepen.io\/\/yowainwright\/pen\/VKVXRa\" rel=\"nofollow\">CodePen demo<\/a> too.<\/p>\n<figure><a target=\"_blank\" href=\"https:\/\/dollarshaveclub.github.io\/shave\/\"><img decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/shave-plugin\/02-truncate-text-example.jpg\" alt=\"truncated text plugin\"><\/a><\/figure>\n<p>Shave is built to <strong>work on jQuery or Zepto<\/strong> if you prefer either of those libraries. But since it also <strong>runs on vanilla JS<\/strong> it\u2019s one of the easiest plugins to drop into your site and start using.<\/p>\n<p>There aren\u2019t too many scenarios where you\u2019ll want to truncate text, but when you do it\u2019s a lot easier to use a plugin like <a target=\"_blank\" href=\"https:\/\/dollarshaveclub.github.io\/shave\/\">Shave<\/a> than to write all the code yourself.<\/p>\n<p>To get started, download a copy from the <a target=\"_blank\" href=\"https:\/\/github.com\/dollarshaveclub\/shave\">GitHub repo<\/a> or pull from a repo like npm. You\u2019ll also find guidelines and documentation on the GitHub repo so you can literally just copy, paste, and get shaving!<\/p>","protected":false},"excerpt":{"rendered":"<p>Most WordPress blogs use the &#8220;read more&#8221; feature to show preview text from a post. This text is truncated and cut off at a certain point to save space and to encourage readers to click further to keep reading. But sometimes you\u2019ll want to add this feature onto a single page. Enter Shave.js, the JavaScript&hellip;<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[352],"tags":[4117,510,511],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Dynamic Truncated Text with Shave.js Plugin - Hongkiat<\/title>\n<meta name=\"description\" content=\"Most WordPress blogs use the &quot;read more&quot; feature to show preview text from a post. This text is truncated and cut off at a certain point to save\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dynamic Truncated Text with Shave.js Plugin\" \/>\n<meta property=\"og:description\" content=\"Most WordPress blogs use the &quot;read more&quot; feature to show preview text from a post. This text is truncated and cut off at a certain point to save\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2016-12-27T13:01:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-22T16:13:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/shave-plugin\/01-shave-plugin-homepage.jpg\" \/>\n<meta name=\"author\" content=\"Jake Rocheleau\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jake Rocheleau\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/\"},\"author\":{\"name\":\"Jake Rocheleau\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/966b2daea15283b4145e71aa98a82c2a\"},\"headline\":\"Dynamic Truncated Text with Shave.js Plugin\",\"datePublished\":\"2016-12-27T13:01:13+00:00\",\"dateModified\":\"2017-11-22T16:13:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/\"},\"wordCount\":329,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/shave-plugin\\\/01-shave-plugin-homepage.jpg\",\"keywords\":[\"Javascripts\",\"Web Designers\",\"Web Developers\"],\"articleSection\":[\"Web Design\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/\",\"name\":\"Dynamic Truncated Text with Shave.js Plugin - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/shave-plugin\\\/01-shave-plugin-homepage.jpg\",\"datePublished\":\"2016-12-27T13:01:13+00:00\",\"dateModified\":\"2017-11-22T16:13:09+00:00\",\"description\":\"Most WordPress blogs use the &quot;read more&quot; feature to show preview text from a post. This text is truncated and cut off at a certain point to save\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/shave-plugin\\\/01-shave-plugin-homepage.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/shave-plugin\\\/01-shave-plugin-homepage.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/shave-plugin\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Dynamic Truncated Text with Shave.js Plugin\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/966b2daea15283b4145e71aa98a82c2a\",\"name\":\"Jake Rocheleau\",\"description\":\"Jake is a writer and designer with over 10 years experience working on the web. He writes about user experience design and cool resources for designers\",\"sameAs\":[\"https:\\\/\\\/www.hongkiat.com\"],\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/jake\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Dynamic Truncated Text with Shave.js Plugin - Hongkiat","description":"Most WordPress blogs use the &quot;read more&quot; feature to show preview text from a post. This text is truncated and cut off at a certain point to save","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/","og_locale":"en_US","og_type":"article","og_title":"Dynamic Truncated Text with Shave.js Plugin","og_description":"Most WordPress blogs use the &quot;read more&quot; feature to show preview text from a post. This text is truncated and cut off at a certain point to save","og_url":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2016-12-27T13:01:13+00:00","article_modified_time":"2017-11-22T16:13:09+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/shave-plugin\/01-shave-plugin-homepage.jpg","type":"","width":"","height":""}],"author":"Jake Rocheleau","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Jake Rocheleau","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/"},"author":{"name":"Jake Rocheleau","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/966b2daea15283b4145e71aa98a82c2a"},"headline":"Dynamic Truncated Text with Shave.js Plugin","datePublished":"2016-12-27T13:01:13+00:00","dateModified":"2017-11-22T16:13:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/"},"wordCount":329,"commentCount":1,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/shave-plugin\/01-shave-plugin-homepage.jpg","keywords":["Javascripts","Web Designers","Web Developers"],"articleSection":["Web Design"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/shave-plugin\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/","url":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/","name":"Dynamic Truncated Text with Shave.js Plugin - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/shave-plugin\/01-shave-plugin-homepage.jpg","datePublished":"2016-12-27T13:01:13+00:00","dateModified":"2017-11-22T16:13:09+00:00","description":"Most WordPress blogs use the &quot;read more&quot; feature to show preview text from a post. This text is truncated and cut off at a certain point to save","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/shave-plugin\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/shave-plugin\/01-shave-plugin-homepage.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/shave-plugin\/01-shave-plugin-homepage.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/shave-plugin\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Dynamic Truncated Text with Shave.js Plugin"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/966b2daea15283b4145e71aa98a82c2a","name":"Jake Rocheleau","description":"Jake is a writer and designer with over 10 years experience working on the web. He writes about user experience design and cool resources for designers","sameAs":["https:\/\/www.hongkiat.com"],"url":"https:\/\/www.hongkiat.com\/blog\/author\/jake\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-7uG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=28810"}],"version-history":[{"count":2,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28810\/revisions"}],"predecessor-version":[{"id":41756,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28810\/revisions\/41756"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=28810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=28810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=28810"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=28810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}