{"id":143,"date":"2020-01-19T14:47:13","date_gmt":"2020-01-19T14:47:13","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.oxyset.com\/?p=143"},"modified":"2026-10-10T18:34:48","modified_gmt":"2026-10-10T18:34:48","slug":"change-default-excerpt","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/change-default-excerpt\/","title":{"rendered":"Change the WordPress Excerpt [\u2026] to a Read More Link"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">WordPress ends every automatic excerpt with <code>&#91;\u2026&#93;<\/code>, which tells the reader there is more but gives them nothing to click. This snippet changes the WordPress default excerpt <code>&#91;\u2026&#93;<\/code> to an ellipsis followed by a <strong>Read more<\/strong> link to the post.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The link always points at the post the excerpt belongs to, also in widgets and custom queries outside the main loop, and screen readers hear which post it opens.<\/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 * Replace the WordPress excerpt \"&#91;\u2026&#93;\" with a \"Read more\" link to the post.\n *\/\n\n\/\/ ===== Config =====\nfunction dpc_excerpt_more_text() {\n\treturn __( 'Read more', 'dpc' ); \/\/ Link text.\n}\n\/\/ ===== End of config =====\n\n\/\/ Remember which post the excerpt is being built for; excerpt_more itself does not get it.\nadd_filter( 'get_the_excerpt', 'dpc_excerpt_more_remember_post', 9, 2 );\n\nfunction dpc_excerpt_more_remember_post( $excerpt, $post = null ) {\n\t$GLOBALS&#91;'dpc_excerpt_more_post'&#93; = $post;\n\treturn $excerpt;\n}\n\nadd_filter( 'excerpt_more', 'dpc_excerpt_more' );\n\nfunction dpc_excerpt_more( $more ) {\n\t$post = get_post( $GLOBALS&#91;'dpc_excerpt_more_post'&#93; ?? null );\n\tif ( ! $post ) {\n\t\treturn $more;\n\t}\n\n\t$text  = dpc_excerpt_more_text();\n\t$title = html_entity_decode( wp_strip_all_tags( get_the_title( $post ) ), ENT_QUOTES, get_bloginfo( 'charset' ) );\n\n\treturn sprintf(\n\t\t'&amp;hellip; &lt;a class=\"dpc-read-more\" href=\"%1$s\" aria-label=\"%2$s\"&gt;%3$s&lt;\/a&gt;',\n\t\tesc_url( get_permalink( $post ) ),\n\t\t\/* translators: 1: link text, 2: post title. *\/\n\t\tesc_attr( sprintf( __( '%1$s: %2$s', 'dpc' ), $text, $title ) ),\n\t\tesc_html( $text )\n\t);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When a post has no hand-written excerpt, <code>wp_trim_excerpt()<\/code> cuts the content and appends the value of the <code>excerpt_more<\/code> filter. The snippet returns <code>&amp;hellip;<\/code> and the link instead of <code> &#91;&amp;hellip;&#93;<\/code>.<\/li>\n\n\n<li><code>excerpt_more<\/code> does not say which post is being trimmed, and <code>get_the_permalink()<\/code> without an argument reads the global post. Outside the loop that can be a different post. The <code>get_the_excerpt<\/code> callback at priority 9 remembers the real post first, and the link is built for that post.<\/li>\n\n\n<li>The URL goes through <code>esc_url()<\/code> and the text through <code>esc_html()<\/code>.<\/li>\n\n\n<li>Many &#8220;Read more&#8221; links in a list sound the same to a screen reader. The <code>aria-label<\/code> starts with the visible text and adds the post title, for example &#8220;Read more: Hello world&#8221;.<\/li>\n\n\n<li>The link has the class <code>dpc-read-more<\/code> for styling. It no longer has <code>rel=\"nofollow\"<\/code>: these are links to your own posts, and search engines should follow them.<\/li>\n\n<\/ul>\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>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor, edit the config and publish. Leave <strong>Action\/Hook<\/strong> empty. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Other wording: change the string in <code>dpc_excerpt_more_text()<\/code>. It is translatable with the <code>dpc<\/code> text domain.<\/li>\n\n\n<li>No ellipsis: remove <code>&amp;hellip; <\/code> at the start of the format string, but keep a space before <code>&lt;a<\/code>.<\/li>\n\n\n<li>Shorter or longer excerpts: 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 in WordPress<\/a>.<\/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>Hand-written excerpts are not trimmed, so they get no link. Show a separate link in your template if you need one there too.<\/li>\n\n\n<li>The block themes&#8217; <strong>Post Excerpt<\/strong> block has its own <strong>More text<\/strong> setting. When it is filled in, the block hides this link so only one is shown.<\/li>\n\n\n<li>Themes that already add a Read more button below the excerpt will now show two links; remove one of them.<\/li>\n\n\n<li>The link also appears in RSS feeds that use excerpts, which is usually what you want.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The older version of this post printed <code>get_the_permalink()<\/code> unescaped, linked to the global post, had no space before the link and added <code>rel=\"nofollow\"<\/code>.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Replace the [&#8230;] at the end of WordPress excerpts with an accessible Read more link to the right post, escaped and translatable.<\/p>\n","protected":false},"author":1,"featured_media":46685,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Change the WordPress Excerpt [\u2026] to a Read More Link","description":"Replace the [...] at the end of WordPress excerpts with an accessible Read more link to the right post, escaped and translatable."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[],"dpc_library":[204],"class_list":["post-143","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\/143","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=143"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":46684,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/143\/revisions\/46684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46685"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=143"}],"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=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=143"},{"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=143"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}