{"id":173,"date":"2020-01-26T07:11:37","date_gmt":"2020-01-26T07:11:37","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.oxyset.com\/?p=173"},"modified":"2026-10-10T20:51:16","modified_gmt":"2026-10-10T20:51:16","slug":"custom-striped-excerpt","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/custom-striped-excerpt\/","title":{"rendered":"WordPress Excerpt Without HTML Tags and a Word Limit"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A WordPress excerpt without HTML tags is what most card layouts, meta descriptions and builder templates need: plain text, a fixed number of words, no stray <code>&lt;p&gt;<\/code>, links or shortcode output, and no theme &#8220;Read more&#8221; link glued to the end.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet prints exactly that for the current post. It uses the manual excerpt when the post has one and otherwise the post content, removes blocks&#8217; markup, shortcodes and tags, and cuts it to the number of words you set.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Plain-text excerpt of the current post: no HTML, no shortcodes, a fixed word count.\n * Scripts Organizer: Trigger location Conditions, Script location Shortcode.\n *\/\n$dpc_words = 30;          \/\/ Maximum number of words.\n$dpc_more  = '&amp;hellip;';  \/\/ Appended when the text was cut. '' for nothing.\n\n$dpc_excerpt_post = get_post();\nif ( $dpc_excerpt_post instanceof WP_Post &amp;&amp; ! post_password_required( $dpc_excerpt_post ) ) {\n\t\/\/ The manual excerpt if there is one, otherwise the content.\n\t$dpc_text = has_excerpt( $dpc_excerpt_post ) ? $dpc_excerpt_post-&gt;post_excerpt : $dpc_excerpt_post-&gt;post_content;\n\t$dpc_text = excerpt_remove_blocks( strip_shortcodes( $dpc_text ) );\n\t$dpc_text = wp_trim_words( $dpc_text, $dpc_words, $dpc_more ); \/\/ Also strips all tags.\n\n\tif ( '' !== $dpc_text ) {\n\t\techo '&lt;p class=\"dpc-excerpt\"&gt;' . esc_html( html_entity_decode( $dpc_text, ENT_QUOTES, 'UTF-8' ) ) . '&lt;\/p&gt;';\n\t}\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Conditions<\/strong>, tick <strong>Shortcode<\/strong> under <strong>Script location<\/strong> (leave <strong>PHP<\/strong> unticked), paste the code into the PHP editor, keep <strong>Template<\/strong> on <strong>All<\/strong> and publish.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The block now shows its shortcode, for example <code>&#91;scorg_shortcode id=\"123\"&#93;<\/code>. Paste it where the output should appear: a Shortcode block in the post or in a block theme template, or the shortcode element of your page builder&#8217;s single or archive template.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inside a Query Loop or a builder repeater the shortcode prints the excerpt of each post in the loop, on a single post template the excerpt of that post. Scripts Organizer includes this file every time the shortcode renders, inside its own function. That is why the code has no function declarations (a second shortcode on the same page would fatal with &#8220;Cannot redeclare&#8221;), uses <code>get_the_ID()<\/code> or <code>get_queried_object_id()<\/code> instead of a bare <code>$post<\/code>, and prefixes its variables with <code>dpc_<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>get_post()<\/code> is the post of the current loop iteration, so it works on single templates and inside loops. Password protected posts print nothing, like WordPress&#8217; own excerpt.<\/li>\n\n\n<li>The text is read from the raw <code>post_excerpt<\/code> or <code>post_content<\/code>. It does not call <code>get_the_excerpt()<\/code>, which runs the excerpt filters and would add your theme&#8217;s &#8220;Continue reading&#8221; text.<\/li>\n\n\n<li><code>strip_shortcodes()<\/code> and <code>excerpt_remove_blocks()<\/code> drop shortcodes and blocks that don&#8217;t belong in an excerpt, <code>wp_trim_words()<\/code> strips every tag and HTML comment and cuts to <code>$dpc_words<\/code> words, adding <code>$dpc_more<\/code> only when it actually cut something.<\/li>\n\n\n<li>The result is decoded and escaped once with <code>esc_html()<\/code>, so <code>&amp;<\/code> in the content prints as <code>&amp;amp;<\/code> and never as <code>&amp;amp;amp;<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Length: change <code>$dpc_words<\/code>. To change the length of the normal WordPress excerpt everywhere instead, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/change-wordpress-default-excerpt-length\/\">Change the default excerpt length<\/a>.<\/li>\n\n\n<li>Ending: set <code>$dpc_more<\/code> to <code>''<\/code> for none, or <code>' ...'<\/code>. To turn the default <code>&#91;\u2026&#93;<\/code> into a link, see &#91;Change the excerpt &#91;\u2026&#93; to a read more link&#93;(https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/change-default-excerpt\/).<\/li>\n\n\n<li>Different wrapper: change the <code>p<\/code> and its <code>dpc-excerpt<\/code> class in the <code>echo<\/code> line.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Text that blocks render dynamically, such as a Query Loop or a synced pattern inside the post, is not in <code>post_content<\/code> and does not appear in the excerpt.<\/li>\n\n\n<li><code>wp_trim_words()<\/code> counts words by spaces. For Chinese, Japanese or Thai it counts characters instead, which WordPress decides from the site language.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Print a WordPress excerpt without HTML tags, shortcodes or a read-more link, cut to a word limit, for cards and templates.<\/p>\n","protected":false},"author":1,"featured_media":46816,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"WordPress Excerpt Without HTML Tags and a Word Limit","description":"Print a WordPress excerpt without HTML tags, shortcodes or a read-more link, cut to a word limit, for cards and templates."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[],"dpc_library":[204],"class_list":["post-173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":46815,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/173\/revisions\/46815"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46816"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=173"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=173"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}