{"id":78,"date":"2025-02-22T22:58:57","date_gmt":"2025-02-22T22:58:57","guid":{"rendered":"https:\/\/notes.zj1963.com\/?p=78"},"modified":"2025-02-22T22:58:57","modified_gmt":"2025-02-22T22:58:57","slug":"list-posts-grouped-by-tag-or-category","status":"publish","type":"post","link":"https:\/\/notes.zj1963.com\/?p=78","title":{"rendered":"List Posts Grouped by Tag or Category"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>How to List WordPress Posts Grouped by Tag or Category<\/strong><\/h3>\n\n\n\n<p>If you want to display WordPress posts <strong>grouped by category or tag<\/strong>, here are the best ways:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1\ufe0f\u20e3 Use a Plugin (Easy Method)<\/strong><\/h2>\n\n\n\n<p>If you prefer a no-code solution, try <strong>Display Posts<\/strong> or <strong>WP Show Posts<\/strong> plugin.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 Steps to Use Display Posts Plugin<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Install &amp; Activate<\/strong> <a href=\"https:\/\/wordpress.org\/plugins\/display-posts\/\">Display Posts<\/a> plugin.<\/li>\n\n\n\n<li>Use this <strong>shortcode<\/strong> in a post\/page:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   &#91;display-posts category=\"your-category\" include_date=\"true\"]<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>Replace <code>\"your-category\"<\/code> with the actual category slug.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2\ufe0f\u20e3 Use Custom PHP Code (For Theme or Custom Template)<\/strong><\/h2>\n\n\n\n<p>If you want to <strong>list posts grouped by category<\/strong> dynamically, add this PHP code to your <strong>theme template (e.g., page.php, sidebar.php, or a custom template):<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 Group Posts by Category<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$categories = get_categories();\nforeach ($categories as $category) {\n    echo '&lt;h2&gt;' . esc_html($category-&gt;name) . '&lt;\/h2&gt;'; \n    $posts = get_posts(array(\n        'category' =&gt; $category-&gt;term_id,\n        'posts_per_page' =&gt; -1 \n    ));\n    foreach ($posts as $post) {\n        echo '&lt;a href=\"' . get_permalink($post-&gt;ID) . '\"&gt;' . esc_html($post-&gt;post_title) . '&lt;\/a&gt;&lt;br&gt;';\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>This will list posts under each category<\/strong> with clickable links.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 Group Posts by Tag<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$tags = get_tags();\nforeach ($tags as $tag) {\n    echo '&lt;h2&gt;' . esc_html($tag-&gt;name) . '&lt;\/h2&gt;'; \n    $posts = get_posts(array(\n        'tag' =&gt; $tag-&gt;slug,\n        'posts_per_page' =&gt; -1 \n    ));\n    foreach ($posts as $post) {\n        echo '&lt;a href=\"' . get_permalink($post-&gt;ID) . '\"&gt;' . esc_html($post-&gt;post_title) . '&lt;\/a&gt;&lt;br&gt;';\n    }\n}\n?&gt;<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>This will list posts grouped by tag.<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3\ufe0f\u20e3 Display Posts Grouped by Category in a Gutenberg Block<\/strong><\/h2>\n\n\n\n<p>If you\u2019re using <strong>Gutenberg Block Editor<\/strong>, you can use the <strong>Query Loop block<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to <strong>Edit Page\/Post<\/strong> \u2192 Click &#8220;+&#8221; to add a block.<\/li>\n\n\n\n<li>Search for <strong>Query Loop<\/strong> and insert it.<\/li>\n\n\n\n<li>Choose <strong>&#8220;Inherit query from template&#8221;<\/strong> or select a <strong>specific category\/tag<\/strong>.<\/li>\n\n\n\n<li>Customize layout and styles.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\ud83c\udfaf Best Option for You?<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2705 <strong>Use a plugin<\/strong> if you want a fast and easy way (e.g., Display Posts).<\/li>\n\n\n\n<li>\u2705 <strong>Use PHP Code<\/strong> if you need full customization in your theme.<\/li>\n\n\n\n<li>\u2705 <strong>Use Gutenberg Query Loop Block<\/strong> for a <strong>native<\/strong> WordPress approach.<\/li>\n<\/ul>\n\n\n\n<p>Would you like help styling the output for a better look? \ud83d\ude0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to List WordPress Posts Grouped by Tag or Category If you want to display WordPress posts grouped by category or tag, here are the best ways: 1\ufe0f\u20e3 Use a Plugin (Easy Method) If you prefer a no-code solution, try Display Posts or WP Show Posts plugin. \ud83d\udd39 Steps to Use Display Posts Plugin 2\ufe0f\u20e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78"}],"version-history":[{"count":1,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions\/79"}],"wp:attachment":[{"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notes.zj1963.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}