{"id":108890,"date":"2022-11-24T05:45:00","date_gmt":"2022-11-24T11:45:00","guid":{"rendered":"https:\/\/wpengine.com\/?post_type=resource&#038;p=108890"},"modified":"2024-09-29T09:24:38","modified_gmt":"2024-09-29T14:24:38","slug":"list-posts-by-category","status":"publish","type":"resource","link":"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/","title":{"rendered":"How to List Posts by Category in WordPress"},"content":{"rendered":"\n<p>Few things break the flow of browsing through a website quite like having to scroll through a long list of unrelated posts. Suddenly, rather than clicking through to the next page, your user is hitting the close button and heading to a different site.<\/p>\n\n\n\n<p>The solution is simple: narrow the focus of displayed posts to a particular WordPress category. Now, a potentially annoying UI element that breaks focus becomes a powerful tool for encouraging users to explore deeper into your site.<\/p>\n\n\n\n<p>There are a few ways to list posts by category in WordPress. You can use a code snippet, or you can employ a WordPress plugin to simplify the process. <\/p>\n\n\n\n<p>In this article, we\u2019ll walk you through exactly how to implement both methods. Let\u2019s get started!<\/p>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Why display posts by category?<\/h2>\n\n\n\n<p>There are a number of reasons you might want to display posts from <a href=\"https:\/\/wpengine.com\/resources\/\/wordpress-categories-and-tags\/\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">a particular category<\/a>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can tailor the suggested posts to be more relevant to a given page. This makes it more likely that readers will click through to additional content.<\/li>\n\n\n\n<li>You can create a special WordPress category for posts you want to highlight and display them across your site, drawing attention and traffic their way. This is especially useful for featuring important posts from a category on the front page of your WordPress site.<\/li>\n\n\n\n<li>You can improve the overall flow of your site. The default \u201crecent posts\u201d widget that comes with WordPress simply displays the latest posts, regardless of category or relevance. This is not ideal from a UX standpoint. If the listed content isn\u2019t relevant, it becomes an obstacle the user has to get around, rather than something that enhances the experience.<\/li>\n<\/ul>\n\n\n\n<p>These are just a few of the ways you can use this technique, but they illustrate how valuable it can be. With that in mind, let\u2019s look at how to get your posts listed by category, starting with the manual approach.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">List posts by category by using a code snippet<\/h2>\n\n\n\n<p>The first method we\u2019ll cover is using a code snippet that instructs WordPress to show all posts in a specific category. The main reason to use a snippet over a WordPress plugin is to get maximum flexibility. If you\u2019re comfortable with code, you can incorporate just about any features you want into your WordPress post list. You can also style it however you like.<\/p>\n\n\n\n<p>For our example, we\u2019ll create a shortcode that displays the last five posts from your chosen category. You can insert this shortcode into <a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/wpengine.com\/resources\/\/what-are-wordpress-widgets-how-to-use-them\/\" target=\"_blank\" rel=\"noreferrer noopener\">any widget area<\/a> using a text widget, or add it to a WordPress post or page using the shortcode widget in the Block Editor.<\/p>\n\n\n\n<p>To create the shortcode, first navigate to <a href=\"https:\/\/codex.wordpress.org\/Functions_File_Explained\" target=\"_blank\" rel=\"noreferrer noopener\">your theme\u2019s <em>functions.php<\/em> file<\/a>. You can find this by going to your WordPress dashboard and navigating to <em>Appearance &gt; Theme Editor<\/em>. The <em>functions.php<\/em> file will likely be titled <em>Theme Functions<\/em> in the list to the right:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/bg3hKsICP9_qDlyE1RmsgwJvmrjQEhMuaPCqo8MZ8TIF3jACT_0J8k6gJ7QP03eXA4H8-wZc69ZuiVViYmfwk3rTtvooT3-Ni5RMa12bxIYeKN-3I3Q3_pqjC0AD6y3iFV5t5oFJ\" alt=\"create shortcode edit themes. WordPress List Posts By Category\"\/><\/figure>\n\n\n\n<p>Next, paste <a href=\"https:\/\/wordpress.stackexchange.com\/questions\/93798\/show-last-5-posts-from-specific-category\/93805#93805\" target=\"_blank\" rel=\"noreferrer noopener\">this code snippet<\/a> at the bottom of the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Last5posts()   {\n    $args = array( 'posts_per_page' =&gt; 5, 'category_name' =&gt; 'uncategorized');                  \n    $last_5_posts_query = new WP_Query( $args );\n    while($last_5_posts_query-&gt;have_posts()) : \n        $last_5_posts_query-&gt;the_post();\n        $link = get_permalink();\n        $title = get_the_title();\n        $date = get_the_date();                              \n\n        $content .= '&lt;div class=\"latest-posts\"&gt;';\n        $content .= '&lt;h3&gt;&lt;a href='.$link.' target=\"_top\"&gt;'.$title.' \/ '.$date. '&lt;\/a&gt;&lt;\/h3&gt;';\n        $content .= '&lt;p class=\"excerpt\"&gt;' .get_the_excerpt(). '&lt;\/p&gt;';\n        $content .= '&lt;\/div&gt;';\n    endwhile;\n\nreturn $content;\n}\n\nadd_shortcode('Last5Posts', 'Last5posts' );<\/code><\/pre>\n\n\n\n<p>Make sure to replace the category name in the third line with your desired category. This snippet will create the shortcode <em>[Last5Posts]<\/em>, which will display the most recent five posts from the selected category.<\/p>\n\n\n\n<p>Once that\u2019s done, simply click <em>Update<\/em> at the bottom of the page and you\u2019re all set. Now you can <a href=\"https:\/\/wpengine.com\/resources\/\/your-shortcode-is-showing\/\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">insert this shortcode<\/a> into a text widget or shortcode widget, to display the latest relevant posts:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/kWQ72Zvt9fbVtquoqArQSUJC8Uf_vQs-gohiV7LWmgFedzrMaj1_3woJO3eqlF4biHsBFewdJ4dv56yitxi2Y_E66WMyOik4gW_KhOurbrIwklWr-mhHh6ADxZ9HeP-8hk43Re3F\" alt=\"insert shortcode in text widget. WordPress List Posts By Category\"\/><\/figure>\n\n\n\n<p>To create multiple shortcodes for different categories, just paste the snippet into your <em>functions.php <\/em>file again. All you need to do is modify the shortcode details.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">List posts by category by using a widget plugin<\/h2>\n\n\n\n<p>If the mention of code snippets makes you uncomfortable, or you prefer a simpler solution that doesn\u2019t require altering your site\u2019s files, don\u2019t worry. There are a number of excellent WordPress plugins that list posts by category. Many of them also offer robust configuration and customization options, so you\u2019re not giving up much by going this route.&nbsp;<\/p>\n\n\n\n<p>If you\u2019re not sure where to start, you can try out the <a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/wordpress.org\/plugins\/recent-posts-widget-extended\/\" target=\"_blank\" rel=\"noreferrer noopener\">Recent Posts Widget Extended<\/a> plugin. This tool adds a block to your editor that you can insert anywhere to display posts from any category (or all of them). It\u2019s a little older, however, and hasn&#8217;t been tested with the latest version of WordPress, so you\u2019ll want to <a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/wpengine.com\/resources\/\/what-is-a-staging-site-why-have-one\/\" target=\"_blank\" rel=\"noreferrer noopener\">test it out carefully<\/a> before using it on your live site.<\/p>\n\n\n\n<p>The plugin gives you a widget for use in sidebars and footers, as well as a block that can be embedded in any page or blog post. The block offers plenty of options: you can adjust the layout, number of posts, whether to show featured image thumbnails, and more:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/hLjGFeDGgdLKegUub361HTqM_kFaY86O7Hkhm77NPTBZrb5iOrJmqeBZXqkEh2tww9ahdi_sJC8DjE4RsnhRJbHBKKQo-Ev18VsXPx88ACfN5KBxRd4qxHH8d-kjEAL9AJEEMso6\" alt=\"list posts by category using widget plugin. WordPress List Posts By Category\"\/><\/figure>\n\n\n\n<p>The widget has a similar set of features, and can be accessed from the <em>Widgets<\/em> section in your <a href=\"https:\/\/wpengine.com\/resources\/\/create-a-wordpress-custom-dashboard\/\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">WordPress dashboard<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/bzPx9c9vnhgI1XwNZE6BwXt7MbR98peGjRXBmYvoEjUU56Ihs_mCzamH9P-Al5pX7M2FiX6FKvBrdEYNfF0L1kUQ0LTfI-4acF4pZZaVb8wQX8pzUHnlAGfkKmhpHOwAxt7AcVQa\" alt=\"plugin widget features in WordPress dashboard. WordPress List Posts By Category\"\/><\/figure>\n\n\n\n<p>Simply customize the parameters however you\u2019d like, and select the <em>Update<\/em> button at the top of the page to save your changes.<\/p>\n\n\n\n<p>If you aren\u2019t a fan of this particular solution, you can easily find more options. There are <a href=\"https:\/\/wpvivid.com\/best-category-plugins.html\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">plenty of plugins<\/a> that let you list posts by category, so feel free to experiment with multiple tools to find the one that works best for you.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Keep your site content optimized with WP Engine<\/h2>\n\n\n\n<p>Displaying a list of posts from a specific category is a smart way to help move visitors further into your WordPress site, and increase traffic to other pages. It also makes for a better user experience all-around.<\/p>\n\n\n\n<p>To create a truly fantastic website, however, you\u2019ll need a strong foundation. Here at WP Engine, we offer <a href=\"https:\/\/wpengine.com\/resources\/\" target=\"_blank\" rel=\"noreferrer noopener\">extensive resources<\/a> to help users and developers create an incredible digital experience for their customers (not to mention <a href=\"https:\/\/wpengine.com\/wordpress-hosting\/\" target=\"_blank\" rel=\"noreferrer noopener\">world-class hosting for WordPress sites<\/a>)!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Few things break the flow of browsing through a website quite like having to scroll through a long list of unrelated posts. Suddenly, rather than clicking through to the next page, your user is hitting the close button and heading to a different site. The solution is simple: narrow the focus of displayed posts to<span class=\"tile__ellipses\">&hellip;<\/span><span class=\"tile__ellipses--animated\"><\/span><\/p>\n","protected":false},"author":1,"featured_media":108891,"template":"","resource-topic":[912,910,901],"resource-role":[896],"resource-type":[916],"class_list":["post-108890","resource","type-resource","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to List Posts by Category in WordPress<\/title>\n<meta name=\"description\" content=\"Showing posts from a specific category on your WordPress site can be a useful way to highlight content. Learn how to do this on your site with WP Engine.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to List Posts by Category in WordPress\" \/>\n<meta property=\"og:description\" content=\"Showing posts from a specific category on your WordPress site can be a useful way to highlight content. Learn how to do this on your site with WP Engine.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Engine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpengine\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-29T14:24:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2020\/08\/image3.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"How to List Posts by Category in WordPress\" \/>\n<meta name=\"twitter:description\" content=\"Showing posts from a specific category on your WordPress site can be a useful way to highlight content. Learn how to do this on your site with WP Engine.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2020\/08\/image3.jpg\" \/>\n<meta name=\"twitter:site\" content=\"@wpengine\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/\",\"url\":\"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/\",\"name\":\"How to List Posts by Category in WordPress\",\"isPartOf\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\"},\"datePublished\":\"2022-11-24T11:45:00+00:00\",\"dateModified\":\"2024-09-29T14:24:38+00:00\",\"description\":\"Showing posts from a specific category on your WordPress site can be a useful way to highlight content. Learn how to do this on your site with WP Engine.\",\"breadcrumb\":{\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/wpengine.com\/case-studies\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resources\",\"item\":\"https:\/\/wpengine.com\/case-studies\/resources\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to List Posts by Category in WordPress\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#website\",\"url\":\"https:\/\/wpengine.com\/case-studies\/\",\"name\":\"WP Engine\",\"description\":\"Managed Hosting for WordPress\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085\",\"name\":\"WP Engine\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g\",\"caption\":\"WP Engine\"},\"sameAs\":[\"https:\/\/wpengine.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to List Posts by Category in WordPress","description":"Showing posts from a specific category on your WordPress site can be a useful way to highlight content. Learn how to do this on your site with WP Engine.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"How to List Posts by Category in WordPress","og_description":"Showing posts from a specific category on your WordPress site can be a useful way to highlight content. Learn how to do this on your site with WP Engine.","og_url":"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/","og_site_name":"WP Engine","article_publisher":"https:\/\/www.facebook.com\/wpengine","article_modified_time":"2024-09-29T14:24:38+00:00","og_image":[{"width":1100,"height":400,"url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2020\/08\/image3.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_title":"How to List Posts by Category in WordPress","twitter_description":"Showing posts from a specific category on your WordPress site can be a useful way to highlight content. Learn how to do this on your site with WP Engine.","twitter_image":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2020\/08\/image3.jpg","twitter_site":"@wpengine","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/","url":"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/","name":"How to List Posts by Category in WordPress","isPartOf":{"@id":"https:\/\/wpengine.com\/case-studies\/#website"},"datePublished":"2022-11-24T11:45:00+00:00","dateModified":"2024-09-29T14:24:38+00:00","description":"Showing posts from a specific category on your WordPress site can be a useful way to highlight content. Learn how to do this on your site with WP Engine.","breadcrumb":{"@id":"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpengine.com\/case-studies\/resources\/list-posts-by-category\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpengine.com\/case-studies\/"},{"@type":"ListItem","position":2,"name":"Resources","item":"https:\/\/wpengine.com\/case-studies\/resources\/"},{"@type":"ListItem","position":3,"name":"How to List Posts by Category in WordPress"}]},{"@type":"WebSite","@id":"https:\/\/wpengine.com\/case-studies\/#website","url":"https:\/\/wpengine.com\/case-studies\/","name":"WP Engine","description":"Managed Hosting for WordPress","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpengine.com\/case-studies\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/f5301455463371a10d1fc290e9ad0085","name":"WP Engine","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wpengine.com\/case-studies\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d8770fe9625ca7c4601f13d9d0ab86565a6dac8cd6a77bfe2ada6d83c6837870?s=96&d=mm&r=g","caption":"WP Engine"},"sameAs":["https:\/\/wpengine.com"]}]}},"acf":[],"grid_image_url":"https:\/\/wpengine.com\/case-studies\/wp-content\/uploads\/2020\/08\/image3.jpg","media-type":{"term_id":916,"name":"Article","slug":"article"},"role":"<strong>Roles:<\/strong> Developer","topic":"<strong>Topics:<\/strong> Performance, SEO, WordPress","_links":{"self":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource\/108890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource"}],"about":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/types\/resource"}],"author":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/users\/1"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media\/108891"}],"wp:attachment":[{"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/media?parent=108890"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-topic?post=108890"},{"taxonomy":"resource-role","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-role?post=108890"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/wpengine.com\/case-studies\/wp-json\/wp\/v2\/resource-type?post=108890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}