Customize archive page URL in Custom Post Type – .com

In this tutorial we will see how to tell WordPress that we want to use an archive page for our CPT and how to customize the URL of said page.

Custom Post Types in WordPress

The Custom Post Types or CPTs are types of posts or personalized content that we can create in WordPress and that can coexist with the types that come by default, such as Posts, Pages or Media.

In this tutorial we are going to start from an already created CPT to which we will activate the option to use archive pages to be able to see the results in the form of a list as we do with blog posts. Later we will modify the URL of said archive page to customize it according to our needs.

If you are interested in the topic, you can visit the tutorial and the , where it is explained in more detail what Custom Post Types are, when to create them and how to do it step by step without using plugins. With all that said, let’s get started!

Activate the use of archive page for a CPT

The first step will be to tell WordPress that we want to use an archive page for our custom post type. If you already have this option enabled, you can go to the next point, otherwise it will be necessary to carry out this previous step because if we do not indicate it, we will not be able to list the content of our Custom Post Type.

See also  Twitch Course #8. Streamer Panel - .com

To activate this option we are going to look for the place where we entered the code with which we created the CPT to be able to modify it. This code can be added to multiple sites, in a new plugin, in a utility plugin, or as a snippet via Code Snippets. It is not recommended to add it to the functions.php file of our theme, so if you have it there you can take the opportunity to change it. In this case, the tutorial on .

Suppose we have code similar to the following. It creates a new CPT called “courses”, in the plural, which is registered as “course” in the singular so that the URLs of the individual courses are of the form domain/course/course-name:

function bld_create_cpt_course() { $args = array( ‘public’ => true, ‘label’ => ‘Courses’ ); register_post_type( ‘course’, $args ); } add_action( ‘init’, ‘bld_create_cpt_course’ );

Well, to indicate that our CPT will use an archive page, we have to add the following line to our function or change the value of “has_archive” to “true” if it already appears in our code:

function bld_create_cpt_course() { $args = array( ‘public’ => true, ‘label’ => ‘Courses’, ‘has_archive’ => true );

Then we will save and then we will have to go to “Settings/Permanent links” and, without having to make any further changes, click on “Save changes”.

And now yes! If we enter domain/course/ we will see that all our listed courses actually appear, so all that remains is to customize their URL. Let’s see how to do it.

Customize CPT Archive Page URL

Once we have indicated that our Custom Post Type has an archive page, we are going to customize the “slug” for its URL. As we have seen, the URL of the individual page of a course has this form domain/course/course-name.

See also  WordPress "Post This" Tool - .com

The word “course” appears in the singular since it is the value with which we have registered our CPT. The same goes for the archive page that lists the available courses and is located at this URL: domain/course/.

How can we change the direction then? It would be nice if the individual pages continued with the singular form and the course archive page was in the plural: domain/courses.

To customize the slug and make our archive page accessible from this new URL we have to modify the code that we have added in the previous point. We will simply change the configuration value of “has_archive” and instead of “true” we will indicate the new slug:

function bld_create_cpt_course() { $args = array( ‘public’ => true, ‘label’ => ‘Courses’, ‘has_archive’ => ‘courses’ );

We save, and again we go to “Settings/Permanent links” and, without making any changes, we save the changes again. Now we access the new URL and that’s it!

With these simple steps we would already have the URL of the Custom Post Type archive page according to our preferences. Also, if you want, you can create your own archive page and layout it according to your interests, since in this case the one that comes by default in our theme is being used.

Summary and conclusion

When defining a Custom Post Type in WordPress we must expressly indicate if we want to use an archive page, otherwise we will not have it available. To show it we will have to add a specific line of code to the arguments that are passed to WordPress when registering our CPT, save, and then go to the “Permalinks” option and save again.

See also  Business Management and Organization Course #3. Management styles in a company - .com

To customize the URL of said page, we would simply have to slightly modify the line that we introduced in the previous step as we have seen in the tutorial and save the permalinks.

And we would have it! If you want to learn more about Custom Post Type, besides the second class of the , we have the and the . And you already know, at you have them available. If you are interested, we are waiting for you inside! 🙂

Loading Facebook Comments ...
Loading Disqus Comments ...