{"id":21316,"date":"2021-03-13T17:00:26","date_gmt":"2021-03-13T16:00:26","guid":{"rendered":"https:\/\/qodeinteractive.com\/magazine\/?p=21316"},"modified":"2023-05-30T15:21:28","modified_gmt":"2023-05-30T13:21:28","slug":"how-to-create-custom-wordpress-archive-page","status":"publish","type":"post","link":"https:\/\/qodeinteractive.com\/magazine\/how-to-create-custom-wordpress-archive-page\/","title":{"rendered":"How to Create a Custom WordPress Archives Page"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><p>[vc_row][vc_column][vc_column_text]Older posts inevitably get buried by chronologically newer content. This is especially the case if your website has grown or if you have been blogging for a while now, and your older articles aren\u2019t getting enough notice. Since older articles could still contain evergreen content, WordPress site owners resort to various techniques of bringing attention to them. One of those techniques is simply using the default WordPress archive page.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Archive pages are enabled thanks to a corresponding template file provided by your theme (archive.php file, in most cases). This is often coupled with post sidebars that add various widgets (like search, categories, and tag cloud), which lead to those pages. However, both the default archive page and the post sidebar have their flaws.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]The default archive pages often only show a list of posts based on a taxonomy without giving additional information about the posts. On the other hand, long sidebars can often cause a sense of clutter on a post and are <a href=\"https:\/\/qodeinteractive.com\/magazine\/how-to-remove-sidebar-in-wordpress\">overall bad for the mobile website experience<\/a>. Because of this, some site owners opt to create a custom WordPress archive page. In this article, we will give our take on how to create a page like that.[\/vc_column_text][vc_empty_space height=&#8221;80px&#8221;][vc_widget_sidebar sidebar_id=&#8221;new-top-picks-banner&#8221;][vc_empty_space height=&#8221;80px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\">How to create a custom WordPress archives page<\/h2>\n<p>[\/vc_column_text][vc_column_text]Now that you know why a custom archives page could be helpful, let us see how you can create it. For this article, we have created a custom archive page using code. Given the use of coding, this task is better suited for more advanced WordPress users. However, we will also explain the code we use, part by part, so that a wider audience can understand it. We created an example that should be instructive and easy for you to use or improve upon, depending on your WordPress comfort level.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]As with our previous articles, we strongly suggest <a href=\"https:\/\/qodeinteractive.com\/magazine\/how-to-manually-backup-wordpress-website\/\">creating a backup of your website<\/a> before going any further. This will ensure that no harm is done to your website and give you peace of mind in going forward. Once you\u2019ve done so, we also advise reviewing our article on <a href=\"https:\/\/qodeinteractive.com\/magazine\/how-to-use-ftp\/\">the use of FTP<\/a>, as knowing how to use FTP is crucial for some of the steps described below. With that being said, let us proceed.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]The code that we created to make our custom WordPress archive page is given below.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php\r\n\/* Template Name: Custom Archive Template *\/\r\nget_header();\r\n?&gt;\r\n&lt;main id=\"main\" class=\"content-wrapper\"&gt;\r\n&lt;?php if ( have_posts() ) {\r\nwhile ( have_posts() ) {\r\nthe_post(); ?&gt;\r\n\r\n&lt;h1 class=\"page-title\"&gt;&lt;?php echo esc_html( get_the_title() ); ?&gt;&lt;\/h1&gt;\r\n&lt;div class=\"page-section\"&gt;\r\n&lt;div class=\"page-content\"&gt;\r\n&lt;?php the_content(); ?&gt;\r\n&lt;\/div&gt;\r\n&lt;?php }\r\nwp_reset_postdata();\r\n} ?&gt;\r\n&lt;div class=\"archive-sidebar\"&gt;\r\n&lt;div class=\"archive-categories\"&gt;\r\n&lt;p&gt;&lt;strong&gt;&lt;?php echo esc_html__( 'Categories', 'textdomain' ); ?&gt;&lt;\/strong&gt;&lt;\/p&gt;\r\n&lt;ul class=\"category-list\"&gt;\r\n&lt;?php wp_list_categories(\r\narray(\r\n'title_li' =&gt; '',\r\n'hide_title_if_empty' =&gt; true\r\n) ); ?&gt;\r\n&lt;\/ul&gt;\r\n&lt;\/div&gt;\r\n&lt;div class=\"archive-tags\"&gt;\r\n&lt;p&gt;&lt;strong&gt;&lt;?php echo esc_html__( 'Tags', 'textdomain' ); ?&gt;&lt;\/strong&gt;&lt;\/p&gt;\r\n&lt;?php wp_tag_cloud(); ?&gt;\r\n&lt;\/div&gt;\r\n&lt;div class=\"archive-authors\"&gt;\r\n&lt;p&gt;&lt;strong&gt;&lt;?php echo esc_html__( 'Authors', 'textdomain' ); ?&gt;&lt;\/strong&gt;&lt;\/p&gt;\r\n&lt;?php wp_list_authors(\r\narray(\r\n'hide_empty' =&gt; 'true',\r\n'optioncount' =&gt; 'true'\r\n) ); ?&gt;\r\n&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;?php $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;\r\n$posts_query = new WP_Query(\r\narray(\r\n'post_type' =&gt; 'post',\r\n'post_status' =&gt; 'publish',\r\n'posts_per_page' =&gt; 8,\r\n'paged' =&gt; $paged\r\n) ); ?&gt;\r\n\r\n&lt;div class=\"posts-section\"&gt;\r\n&lt;?php if ( $posts_query-&gt;have_posts() ) { ?&gt;\r\n&lt;h2&gt;&lt;?php echo esc_html__( 'Our latest work', 'textdomain' ); ?&gt;&lt;\/h2&gt;\r\n&lt;div class=\"archived-posts\"&gt;\r\n&lt;?php while ( $posts_query-&gt;have_posts() ) {\r\n$posts_query-&gt;the_post(); ?&gt;\r\n&lt;div class=\"archive-item\"&gt;\r\n&lt;?php if ( has_post_thumbnail( get_the_ID() ) ) { ?&gt;\r\n&lt;div class=\"post-thumbnail\"&gt;\r\n&lt;a href=\"&lt;?php the_permalink(); ?&gt;\"&gt;\r\n&lt;?php the_post_thumbnail(); ?&gt;\r\n&lt;\/a&gt;\r\n&lt;\/div&gt;\r\n&lt;?php } ?&gt;\r\n&lt;div class=\"post-title\"&gt;\r\n&lt;a href=\"&lt;?php the_permalink(); ?&gt;\"&gt;\r\n&lt;h3&gt;&lt;?php the_title(); ?&gt;&lt;\/h3&gt;\r\n&lt;\/a&gt;\r\n&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;?php } ?&gt;\r\n&lt;\/div&gt;\r\n&lt;?php\r\n$total_pages = $posts_query-&gt;max_num_pages;\r\nif ( $total_pages &gt; 1 ) {\r\n$current_page = max( 1, get_query_var( 'paged' ) ); ?&gt;\r\n&lt;div class=\"archive-pagination\"&gt;\r\n&lt;?php echo paginate_links( array(\r\n'base' =&gt; get_pagenum_link( 1 ) . '%_%',\r\n'format' =&gt; 'page\/%#%',\r\n'current' =&gt; $current_page,\r\n'total' =&gt; $total_pages\r\n) ); ?&gt;\r\n&lt;\/div&gt;\r\n&lt;?php }\r\nwp_reset_postdata();\r\n} else { ?&gt;\r\n&lt;div class=\"archived-posts\"&gt;&lt;?php echo esc_html__( 'No posts matching the query were found.', 'textdomain' ); ?&gt;&lt;\/div&gt;\r\n&lt;?php } ?&gt;\r\n&lt;\/div&gt;\r\n&lt;\/main&gt;\r\n&lt;?php\r\nget_footer();<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Now, let us explain it in more detail. The first step in doing so is to examine it in a very simplified form.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php\r\n\/* Template Name: Custom Archive Template *\/\r\nget_header();\r\n?&gt;\r\n&lt;main id=\"main\" class=\"content-wrapper\"&gt;\r\n&lt;!-- Some code goes here--&gt;\r\n&lt;\/main&gt;\r\n&lt;?php\r\nget_footer();<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]By reviewing the template header comment, you can conclude that the code represents a <a href=\"https:\/\/qodeinteractive.com\/magazine\/create-wordpress-custom-page-template\/\">custom page template<\/a> called Custom Archive Template. Since no additional information is added, this template will be available only for pages and can be selected as a template within the Page Attributes section on a given page.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"518\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Page-Backend.jpg\" class=\"attachment-full size-full\" alt=\"Page Backend\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Page-Backend.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Page-Backend-300x160.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Page-Backend-768x411.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Page-Backend-620x331.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]Apart from that, by using the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_header\/\" target=\"_blank\" rel=\"noopener\">get_header()<\/a> and <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_footer\/\" target=\"_blank\" rel=\"noopener\">get_footer()<\/a> functions, we are loading the header and footer templates of the currently active theme. The remaining code is used to create the main section of the page.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]The main part of the page content consists of three subsections. The first section shows the page title and the content that was inserted in the page editor. The title is displayed thanks to the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_the_title\/\" target=\"_blank\" rel=\"noopener\">get_the_title()<\/a> function, while the page editor content is shown by placing the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/the_content\/\" target=\"_blank\" rel=\"noopener\">the_content()<\/a> function within a <a href=\"https:\/\/developer.wordpress.org\/themes\/basics\/the-loop\/\" target=\"_blank\" rel=\"noopener\">WordPress Loop<\/a>.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php if ( have_posts() ) {\r\nwhile ( have_posts() ) {\r\nthe_post(); ?&gt;\r\n&lt;h1 class=\"page-title\"&gt;&lt;?php echo esc_html( get_the_title() ); ?&gt;&lt;\/h1&gt;\r\n&lt;div class=\"page-section\"&gt;\r\n&lt;div class=\"page-content\"&gt;\r\n&lt;?php the_content(); ?&gt;\r\n&lt;\/div&gt;\r\n&lt;?php }\r\nwp_reset_postdata();\r\n} ?&gt;\r\n&lt;!-- Sidebar code goes here--&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]The second section, which is designed as a sidebar, contains the links to the default WordPress archive pages that show categories, tags, and authors. We have purposely included only those three, as adding too much content might make the page cluttered. However, you can opt to include additional content like <a href=\"https:\/\/qodeinteractive.com\/magazine\/add-widget-area-to-wordpress\/\">custom widget areas<\/a> or links to other archive pages.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]With that being said, each of the aforementioned three parts is preceded by a small paragraph noting what they are (Categories, Tags, and Authors, respectively). The category links are shown thanks to the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_list_categories\/\" target=\"_blank\" rel=\"noopener\">wp_list_categories()<\/a> function, which, as the name suggests, shows a list of category names. As we already used a small paragraph titled Categories, we didn\u2019t want to include an additional title for that category list, so we set the title_li attribute to an empty string. Also, we have opted to hide any empty categories, i.e. categories that don\u2019t have any posts assigned to them.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]The tags are simply shown calling the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_tag_cloud\/\" target=\"_blank\" rel=\"noopener\">wp_tag_cloud()<\/a> function, without specifying any parameters.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Finally, we have shown the authors by calling the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_list_authors\/\" target=\"_blank\" rel=\"noopener\">wp_list_authors()<\/a> function. Additionally, we chose to hide any authors without posts and to show how many posts each author has created. This will be shown within parenthesis, after the author\u2019s name.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;div class=\"archive-sidebar\"&gt;\r\n&lt;div class=\"archive-categories\"&gt;\r\n&lt;p&gt;&lt;strong&gt;&lt;?php echo esc_html__( 'Categories', 'textdomain' ); ?&gt;&lt;\/strong&gt;&lt;\/p&gt;\r\n&lt;ul class=\"category-list\"&gt;\r\n&lt;?php wp_list_categories(\r\narray(\r\n'title_li' =&gt; '',\r\n'hide_title_if_empty' =&gt; true\r\n) ); ?&gt;\r\n&lt;\/ul&gt;\r\n&lt;\/div&gt;\r\n&lt;div class=\"archive-tags\"&gt;\r\n&lt;p&gt;&lt;strong&gt;&lt;?php echo esc_html__( 'Tags', 'textdomain' ); ?&gt;&lt;\/strong&gt;&lt;\/p&gt;\r\n&lt;?php wp_tag_cloud(); ?&gt;\r\n&lt;\/div&gt;\r\n&lt;div class=\"archive-authors\"&gt;\r\n&lt;p&gt;&lt;strong&gt;&lt;?php echo esc_html__( 'Authors', 'textdomain' ); ?&gt;&lt;\/strong&gt;&lt;\/p&gt;\r\n&lt;?php wp_list_authors(\r\narray(\r\n'hide_empty' =&gt; 'true',\r\n'optioncount' =&gt; 'true'\r\n) ); ?&gt;\r\n&lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]The third section, thanks to the <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/\" target=\"_blank\" rel=\"noopener\">WP_Query<\/a> class and a custom query, simply shows all the published posts on your website, starting from the most recent. As your website could have numerous posts, we made it so that this section only shows eight posts per page. It will also have numeric pagination at the bottom, which will help visitors navigate through all the posts. Furthermore, by using the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_query_var\/\" target=\"_blank\" rel=\"noopener\">get_query_var()<\/a> function, we made sure to properly check and display posts from a given page only.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Since the given template is already rich in content, we tried to keep the contents of this section simple. Therefore, if there are published posts available, they will be shown alongside an h2 heading saying <em>Our latest work<\/em>. If there are no published posts, a label stating <em>No posts matching the query were found<\/em>. will be shown instead. The posts are represented by their titles and featured images (if the posts have featured images included). The latter is achieved by using the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/has_post_thumbnail\/\" target=\"_blank\" rel=\"noopener\">has_post_thumbnail()<\/a> conditional statement. Apart from that, both the title and featured image link to the page of the appropriate post, thanks to the use of <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/the_permalink\/\" target=\"_blank\" rel=\"noopener\">the_permalink()<\/a> function. Finally, all labels within this and previous sections are properly <a href=\"https:\/\/developer.wordpress.org\/themes\/theme-security\/data-sanitization-escaping\/#escaping-securing-output\" target=\"_blank\" rel=\"noopener\">escaped for security reasons<\/a> using the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/esc_html\/\" target=\"_blank\" rel=\"noopener\">esc_html()<\/a> function and are made translatable.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;\r\n$posts_query = new WP_Query(\r\narray(\r\n'post_type' =&gt; 'post',\r\n'post_status' =&gt; 'publish',\r\n'posts_per_page' =&gt; 8,\r\n'paged' =&gt; $paged\r\n) ); ?&gt;\r\n&lt;div class=\"posts-section\"&gt;\r\n&lt;?php if ( $posts_query-&gt;have_posts() ) { ?&gt;\r\n&lt;h2&gt;&lt;?php echo esc_html__( 'Our latest work', 'textdomain' ); ?&gt;&lt;\/h2&gt;\r\n&lt;div class=\"archived-posts\"&gt;\r\n&lt;?php while ( $posts_query-&gt;have_posts() ) {\r\n$posts_query-&gt;the_post(); ?&gt;\r\n&lt;div class=\"archive-item\"&gt;\r\n&lt;?php if ( has_post_thumbnail( get_the_ID() ) ) { ?&gt;\r\n&lt;div class=\"post-thumbnail\"&gt;\r\n&lt;a href=\"&lt;?php the_permalink(); ?&gt;\"&gt;\r\n&lt;?php the_post_thumbnail(); ?&gt;\r\n&lt;\/a&gt;\r\n&lt;\/div&gt;\r\n&lt;?php } ?&gt;\r\n&lt;div class=\"post-title\"&gt;\r\n&lt;a href=\"&lt;?php the_permalink(); ?&gt;\"&gt;\r\n&lt;h3&gt;&lt;?php the_title(); ?&gt;&lt;\/h3&gt;\r\n&lt;\/a&gt;\r\n&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;?php } ?&gt;\r\n&lt;\/div&gt;\r\n&lt;?php\r\n\/\/ Pagination code goes here\r\nwp_reset_postdata();\r\n} else { ?&gt;\r\n&lt;div class=\"archived-posts\"&gt;&lt;?php echo esc_html__( 'No posts matching the query were found.', 'textdomain' ); ?&gt;&lt;\/div&gt;\r\n&lt;?php } ?&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Now that we\u2019ve explained that, the only thing that we need to elaborate on is the pagination code. The pagination was done using the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/paginate_links\/\" target=\"_blank\" rel=\"noopener\">paginate_links()<\/a> function, which can be used to create a paginated list of links. Also, we included additional code that ensures that the pagination is only shown in the cases where there are enough posts.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]More precisely, we accessed the <strong>max_num_pages<\/strong> attribute of the custom query named <strong>$posts_query<\/strong> that we created previously. Thanks to it, the navigation is only shown if the number of pages is greater than one. Since we have set the number of posts per page to eight earlier, it means that the pagination is only shown if there are more than eight posts available, which is the expected behavior.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]As for the <strong>paginate_links()<\/strong> function, we only used the following parameters\u2014<strong>base, format, current,<\/strong> and <strong>total<\/strong>. The <strong>base<\/strong> represents the main part of the pagination URL. We have opted to use the get_pagenum_link(1), which represents the link of the first page, i.e. the page that has the custom template enabled. We also appended %_%, which will be replaced with the format parameter.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]And for the <strong>format<\/strong>, we put<strong> page\/%#%<\/strong>, where the %#% part will be replaced with the appropriate pagination number. As an example, if we want to access the second page from the pagination, its URL will be <strong>current-page-URL\/page\/2<\/strong> thanks to the chosen format.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Additionally, we have to mention that, in our case, the URL of the <strong>current<\/strong> page ends with a trailing slash, due to the permalink choice we made on the website. In the case you are using a permalink setting that doesn\u2019t include a trailing slash, you would need to adjust the format. Two solutions that come to mind are either <strong>\/page\/%#% or ?page\/%#%<\/strong>.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]With that being said, the remaining two parameters we used are quite straightforward\u2014<strong>total<\/strong> contains the number of links that will be shown and <strong>current<\/strong> properly keeps track of which page in the pagination a visitor is currently on.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$total_pages = $posts_query-&gt;max_num_pages;\r\nif ( $total_pages &gt; 1 ) {\r\n$current_page = max( 1, get_query_var( 'paged' ) ); ?&gt;\r\n&lt;div class=\"archive-pagination\"&gt;\r\n&lt;?php echo paginate_links( array(\r\n'base' =&gt; get_pagenum_link( 1 ) . '%_%',\r\n'format' =&gt; 'page\/%#%',\r\n'current' =&gt; $current_page,\r\n'total' =&gt; $total_pages\r\n) ); ?&gt;\r\n&lt;\/div&gt;\r\n&lt;?php }<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]This concludes our explanation of the custom code we created.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]However, there is a lot more to cover on how to properly store and use this template. We will delve into that below.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Since the code represents a custom page template, <strong>you will need to put it in a separate file and upload it, via FTP, to your server in one of four possible places<\/strong>. As mentioned in our article on custom page templates, those 4 places are <strong>the directories of your parent or child theme, depending on which you are using, or a subdirectory within either of the two<\/strong>. When creating this example for the article, we used the first option.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Also, you should carefully choose how you name the file. Avoid using the prefix <em>page-<\/em> when naming it. Instead, you should name the file so that its purpose is clear. For example, we named our file <strong>custom-archive-template.php<\/strong>.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Once you upload the file to your server, <strong>you can create a new page on your website and select your newly created page template on it<\/strong>. Then, <strong>you can add the content using your preferred page editor<\/strong>. This content will appear in the first of the three sections we previously mentioned.[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"445\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Choosing-Page-Attribute.jpg\" class=\"attachment-full size-full\" alt=\"Choosing Page Attribute\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Choosing-Page-Attribute.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Choosing-Page-Attribute-300x138.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Choosing-Page-Attribute-768x353.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Choosing-Page-Attribute-620x285.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;38px&#8221;][vc_column_text]Afterward,<strong> examine the output of your page template<\/strong>. Thanks to your theme\u2019s existing stylization, this content should also be partially styled to match. However, you will probably need additional CSS code to further adjust the page style to your liking.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]The creation of this CSS should be done on a case-by-case basis, which is why we can\u2019t provide a definitive solution or supply you with a universal one. Nevertheless, we will share the CSS we created for our example in the hopes you might find parts of it useful and can adjust it to your website. We will also cover the correct programming practices for storing that CSS.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]If you only need a bit of CSS to get the job done, inserting it in Appearance &gt; Customize &gt; Additional CSS is an acceptable solution. However, <a href=\"https:\/\/qodeinteractive.com\/magazine\/how-to-enque-scripts-wordpress\/\">enqueueing your CSS<\/a> using the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_enqueue_style\/\" target=\"_blank\" rel=\"noopener\">wp_enqueue_style()<\/a> function is the proper programming practice, especially if the amount of CSS is larger. As this was the case on our end, we decided to show you the PHP code we used for enqueueing the CSS styles as well. This code can be inserted either inside the functions.php file of your theme or within a <a href=\"https:\/\/qodeinteractive.com\/magazine\/wordpress-site-specific-plugin\/\">site-specific plugin<\/a>. Additionally, for the enqueueing to work properly, you need to create the .css file with the appropriate stylization first and upload it to your server.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Generally speaking, you can upload this .css file in any of the previously mentioned four locations. But, if you plan on making further page templates and subsequent .css files for their stylization, you should consider placing the templates inside a subfolder of your current theme folder. Furthermore, creating a nested folder structure within that subfolder is something you should consider for separating all the files (.css, .js, and .php) within it.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]However, since we are only making this as an example, we\u2019ve uploaded our .css file in the same place as the template file\u2014within the theme folder. The file name that we used was <strong>custom-archive-template-style.css<\/strong>.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Taking all that into account, the appropriate PHP code for enqueueing our .css file is shown below.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">if ( ! function_exists( 'custom_archive_template_styles' ) ) {\r\nfunction custom_archive_template_styles() {\r\nif ( is_page_template( 'custom-archive-template.php' ) ) {\r\nwp_enqueue_style( 'custom-archive-template-style', get_template_directory_uri() . '\/custom-archive-template-style.css' );\r\n}\r\n}\r\n}\r\nadd_action( 'wp_enqueue_scripts', 'custom_archive_template_styles' );<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Let\u2019s quickly review this code as well. First of all, it represents a function called <strong>custom_archive_template_styles()<\/strong> that is \u201chooked\u201d onto a <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/wp_enqueue_scripts\/\" target=\"_blank\" rel=\"noopener\">wp_enqueue_scripts<\/a> hook. This hook is used for enqueueing both the frontend scripts and the styles.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]The function contains a single conditional\u2014<a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/is_page_template\/\" target=\"_blank\" rel=\"noopener\">is_page_template()<\/a> that determines if the specified template is being used and if the condition is met for the enqueueing code to be executed. Simply put, if the template path is given properly, it means that you enqueue the style only on the frontend of a page that uses the specified page template.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]We used two parameters to enqueue the .css file using the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_enqueue_style\/\" target=\"_blank\" rel=\"noopener\">wp_enqueue_style()<\/a> function\u2014a unique handle and a path to the .css file. The path is created using the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_template_directory_uri\/\" target=\"_blank\" rel=\"noopener\">get_template_directory_uri()<\/a> function, which retrieves the URL of the folder of your parent theme, without the trailing slash. Without being said, we will clarify some potential doubts a reader could have regarding this code before going any further.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]First of all, if you have decided to place the template file within a subfolder, you will need to adjust the relative path used when calling the <strong>is_page_template()<\/strong> function to \u2018<em>subfolder-name\/template-file-name.php<\/em>\u2019. Needless to say, the<strong> subfolder-name<\/strong> and <strong>template-file-name.php<\/strong> parts should be replaced with the actual names of your subfolder and template file, respectively.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Additionally, if you are using a child theme, you should replace the get_template_directory_uri() function with <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/get_stylesheet_directory_uri\/\" target=\"_blank\" rel=\"noopener\">get_stylesheet_directory_uri()<\/a>, which returns the URL of your child theme, without the trailing slash. Finally, if your .css file is placed inside a nested subfolder structure, make sure to properly write the remaining part of its URL after using the appropriate function of the two previously mentioned.[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]Having said that, the CSS we used is shown below. But, since it is created on a case-by-case basis, the elements we used might not apply to you, so we will not go into in-depth explanations as we\u2019ve done for the code above. In case you aren\u2019t familiar with some of the CSS rules we used, we advise reading up on them before proceeding.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">.page-section {\r\ndisplay: grid;\r\n}\r\n@media only screen and (min-width: 1025px) {\r\n.page-section {\r\ngrid-template-columns: auto 25%;\r\ngrid-gap: 2rem;\r\n}\r\n}\r\n.page-content, .archive-sidebar {\r\nmargin-bottom: 20px;\r\n}\r\n.archived-posts {\r\ndisplay: grid;\r\ngrid-template-columns: repeat(auto-fill, minmax(300px, 1fr));\r\ngrid-gap: 1.2rem;\r\n}\r\n.post-title {\r\npadding: 6px 0;\r\n}\r\n.archive-pagination {\r\nposition: relative;\r\ntext-align: center;\r\nmargin: 35px 0 0;\r\n}\r\n.page-numbers {\r\nmargin: 0 10px;\r\nfont-size: 17px;\r\nfont-weight: 600;\r\ncolor: #333;\r\nborder-radius: 50%;\r\nline-height: 32px;\r\n}\r\n.page-numbers.current {\r\nbackground: #e82a2a;\r\npadding: 0 9px;\r\n}\r\nul.category-list li {\r\ndisplay: inline-block;\r\n}\r\nul.category-list li, .archive-authors li {\r\nlist-style: none;\r\n}\r\nul.category-list li a {\r\nmargin: 0 10px 5px 0;\r\nfloat: left;\r\nbackground: #eee;\r\ncolor: #333;\r\npadding: 5px 10px;\r\n}\r\nul.category-list li a:hover {\r\nbackground: #e82a2a;\r\ncolor: #fff;\r\n}<\/pre>\n<p>[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]After inserting this CSS, we got the output you can see in the screenshot below.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;50px&#8221;]<div class=\"qodef-single-image-holder   qodef-has-border \">\n    <div class=\"qodef-si-inner\" >\n                                    <img loading=\"lazy\" decoding=\"async\" width=\"969\" height=\"555\" src=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Archive-Preview.jpg\" class=\"attachment-full size-full\" alt=\"Archive Preview\" srcset=\"https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Archive-Preview.jpg 969w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Archive-Preview-300x172.jpg 300w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Archive-Preview-768x440.jpg 768w, https:\/\/qodeinteractive.com\/magazine\/wp-content\/uploads\/2021\/03\/Archive-Preview-620x355.jpg 620w\" sizes=\"auto, (max-width: 969px) 100vw, 969px\" \/>                        <\/div>\n<\/div>[vc_empty_space height=&#8221;78px&#8221;][\/vc_column][\/vc_row][vc_row][vc_column][vc_column_text]<\/p>\n<h2 class=\"qodef-h4\">Final Thoughts<\/h2>\n<p>[\/vc_column_text][vc_column_text]Having a custom archives page can help bring new readers to your posts, especially to the older ones. Additionally, it is a great tool that can help visitors navigate through the various parts of your blog while giving more insight into it. Using a custom WordPress archive page alongside the regular menu navigation can also increase the user experience and eliminate the feeling of being stranded on default archive pages.<br \/>\n[\/vc_column_text][vc_empty_space height=&#8221;28px&#8221;][vc_column_text]As this subject can be explored in various ways, we hope that our example inspires you to create your unique archives page. The code we put together should help you create your Archives page with ease. And, as we included some advanced coding points we hope this article could prove useful in your future coding endeavors.<br \/>\n[\/vc_column_text][\/vc_column][\/vc_row]<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Learn how to create a custom WordPress archive page and make sure your visitors enjoy browsing through your old posts and discovering your content.<\/p>\n","protected":false},"author":11229,"featured_media":21640,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[27,4,13],"class_list":["post-21316","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-content","tag-tips","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts\/21316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/users\/11229"}],"replies":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/comments?post=21316"}],"version-history":[{"count":0,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/posts\/21316\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/media\/21640"}],"wp:attachment":[{"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/media?parent=21316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/categories?post=21316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qodeinteractive.com\/magazine\/wp-json\/wp\/v2\/tags?post=21316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}