1. Home
  2. »
  3. WordPress Tutorials
  4. »
  5. How to Create a Custom Archive Page in WordPress

How to Create a Custom Archive Page in WordPress

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.

Shashika De Silva

Shashika De Silva

Hey there! I’m a seasoned PHP developer with over 10 years of experience crafting awesome WordPress plugins and themes. I specialize in creating scalable and robust solutions for WordPress and WooCommerce, ensuring everything runs smoothly. Whether it’s cross-platform software development, web development, or diving into Sheets/Excel with Appscript, Macros, and VBA, I’ve got you covered. I’m all about delivering top-notch results that go beyond expectations. Let’s team up and turn your ideas into reality, making your project shine! Looking forward to working together and achieving something remarkable!

Select By Category

Flashify.Lab

Join our team
to create the best digital solutions.

Enhance your WordPress site’s functionality with custom plugins tailored to your unique needs. Our expert developers specialize in creating robust plugins that seamlessly integrate with WooCommerce, ensuring a streamlined user experience and enhanced site performance. Transform your ideas into reality with our bespoke plugin development services today

Scroll to Top