{"id":10193,"date":"2022-02-18T19:38:37","date_gmt":"2022-02-18T19:38:37","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/markokrstic.com\/?post_type=code&#038;p=10193"},"modified":"2026-10-10T20:48:26","modified_gmt":"2026-10-10T20:48:26","slug":"order-numbers","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/order-numbers\/","title":{"rendered":"Add Order Numbers to Items in WordPress With CSS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Steps, features or team cards often need a number in front of each item: 1, 2, 3. Typing the numbers by hand in WordPress means renumbering everything when you move or add an item.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet adds order numbers to items automatically. Add the class <code>dpc-numbered<\/code> to the block that holds the items, and each item gets its number in front, in the order it appears on the page.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"css\" class=\"language-css\">\/* Number the direct children of any block with the class dpc-numbered. *\/\n.dpc-numbered {\n\tcounter-reset: dpc-item;\n}\n\n.dpc-numbered &gt; * {\n\tcounter-increment: dpc-item;\n}\n\n.dpc-numbered &gt; *::before {\n\tcontent: counter(dpc-item) \". \";\n\tfont-weight: 700;\n\tmargin-inline-end: 0.25em;\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><code>counter-reset<\/code> starts a counter called <code>dpc-item<\/code> at 0 on each <code>.dpc-numbered<\/code> element, so two numbered groups on one page each start at 1.<\/li>\n\n\n<li>Every direct child of that element increases the counter by one, and its <code>::before<\/code> shows the current value followed by a dot.<\/li>\n\n\n<li>In the block editor, select the Group, Columns, Grid or Query Loop post template block, open <strong>Advanced \u2192 Additional CSS class(es)<\/strong> and type <code>dpc-numbered<\/code>. Its direct children (the blocks inside a Group, the columns, the posts of a loop) are numbered.<\/li>\n\n\n<li>The old version did the same with jQuery: it loaded jQuery, ran after the page had loaded and did not number items added later. CSS counters need no script and number new items too.<\/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>Conditions<\/strong> with <strong>Template<\/strong> set to <strong>All<\/strong>, tick <strong>Header<\/strong> under <strong>Script location<\/strong>, set <strong>Header Mode<\/strong> to <strong>CSS<\/strong>, paste the CSS and publish. Scripts Organizer prints Header and Footer code only with the Conditions trigger on the front end, not with Everywhere.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also paste the CSS into <strong>Appearance \u2192 Editor \u2192 Styles \u2192 Additional CSS<\/strong> or your theme&#8217;s stylesheet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Leading zeros (01, 02): use <code>counter(dpc-item, decimal-leading-zero)<\/code>.<\/li>\n\n\n<li>Start at another number: <code>counter-reset: dpc-item 4;<\/code> makes the first item 5.<\/li>\n\n\n<li>Style the number as a badge: give <code>::before<\/code> a background, <code>border-radius: 50%<\/code> and a fixed width and height, and set <code>display: inline-grid; place-items: center;<\/code>.<\/li>\n\n\n<li>A real list of text is better as a List block set to <strong>Ordered<\/strong>, which numbers itself and is announced as a list by screen readers.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Variant: numbers as real elements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you need the number as an element, for example to animate it or to target it with other CSS, use this JavaScript in the footer (Footer Mode <strong>Javascript<\/strong>) instead of the CSS. Each item gets a <code>&lt;span class=\"dpc-number\"&gt;<\/code> in front:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"js\" class=\"language-js\">document.querySelectorAll('.dpc-numbered').forEach(function (list) {\n\tArray.prototype.forEach.call(list.children, function (item, index) {\n\t\tvar number = document.createElement('span');\n\t\tnumber.className = 'dpc-number';\n\t\tnumber.textContent = String(index + 1);\n\t\titem.prepend(number);\n\t});\n});<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Only direct children are numbered. If your items sit inside an extra wrapper, add the class to that wrapper instead.<\/li>\n\n\n<li>Screen readers usually read generated <code>::before<\/code> content, but it cannot be selected or copied. Use the variant if the number must be part of the text.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Number cards, columns or any repeated items in WordPress automatically with a CSS counter: add one class to the block, no jQuery needed.<\/p>\n","protected":false},"author":1,"featured_media":46796,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Add Order Numbers to Items in WordPress With CSS","description":"Number cards, columns or any repeated items in WordPress automatically with a CSS counter: add one class to the block, no jQuery needed."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[22],"tags":[],"dpc_library":[204],"class_list":["post-10193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10193","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=10193"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10193\/revisions"}],"predecessor-version":[{"id":46795,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10193\/revisions\/46795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46796"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=10193"}],"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=10193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=10193"},{"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=10193"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}