In WordPress, archive pages are used to display a list of posts based on specific criteria such as date, category, tag, author, etc. By default, WordPress provides archive pages for categories, tags, authors, and date-based archives. However, there may be cases where you want to create a custom archive page to display posts in a unique way.
To create a custom archive page in WordPress, you can follow these steps:
Step 1: Create a new template file
<?php /* Template Name: Custom Archive */ get_header(); // Your custom archive page content here get_footer(); ?>
Create a new PHP file in your theme directory and add the above code. You can name this file anything you want, but it’s a good practice to name it something that describes its purpose, like custom-archive.php. In the code, we have defined a custom template named “Custom Archive”. You can customize the content of this file to design your custom archive page layout.
Step 2: Customize the query for the archive page
function flashify_custom_archive_query($query) { if ($query->is_main_query() && is_post_type_archive('your_custom_post_type')) { $query->set('posts_per_page', 10); $query->set('orderby', 'title'); $query->set('order', 'ASC'); } } add_action('pre_get_posts', 'flashify_custom_archive_query');
In the code above, we have created a custom function named “flashify_custom_archive_query” to modify the main query for the custom archive page. You can adjust the parameters like posts_per_page, orderby, and order based on your requirements. Make sure to replace ‘your_custom_post_type’ with the actual post type you want to display on the custom archive page.
Step 3: Create a custom archive page template
function flashify_get_custom_archive_template($template) { if (is_post_type_archive('your_custom_post_type')) { $new_template = locate_template(array('custom-archive.php')); if (!empty($new_template)) { return $new_template; } } return $template; } add_filter('template_include', 'flashify_get_custom_archive_template');
In the code snippet above, we have created a function named “flashify_get_custom_archive_template” to load the custom archive page template we created earlier. This function checks if the current page is a post type archive for ‘your_custom_post_type’ and then returns the custom template file if it exists.
By following these steps, you can create a custom archive page in WordPress to display your posts in a unique and specific way. Remember to customize the template file and query parameters according to your needs to achieve the desired layout and functionality on your custom archive page.