NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

WordPress: Template page Post Type & Redirect or Create An Page Archive Post Type
Easy
Web Hosting Canada

Last modified : Oct 01 2022

Estimated reading time : 2 minutes, 2 seconds - 215 words

With this tutorial, we will create a redirect that forces a Post Type template to use a WordPress page by using template_redirect.

The first step is to create your Post Type page template. For example, if you’re using the term team, you’ll need a template-equipe.php page template.

In your function, you’ll only need one condition: that it needs a page called Your Team that uses the page template created above. Your page needs to have the your-team slug i.e. the full URL should be https://www.domain-name.com/your-team.

What’s interesting with using Post Type template pages is that you can set it apart from the index and archive.php files allowing you to customize it by fetching results from a custom loop query.

Votre page template Post Type (Un exemple) :

<?php
/**
* Template Name: Equipe
* @package WordPress
* @subpackage Nom du Theme
* @since Nom du Theme 1.0
*/
?>
<?php get_header(); ?>
<!--Page start-->
<div class="service_in">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="service_txt">
<div class="col-md-9 col-sm-6 col-xs-12">
<h4></h4>
</div>
<div class="row"></div>
</div>
</div>
</div>
</div>
</div>
<!--Page end--> 

<?php include( get_template_directory() . '/templates-part/part-blog.php'); ?>
<?php get_footer(); ?>

Create a wp_redirect for an archive template page:

<?php
function votretheme_redirect_cpt_archive() {
    if( is_post_type_archive( 'equipe' ) ) {
        wp_redirect( home_url( 'votre-equipe' ), 301 );
        exit();
    }
}
add_action( 'template_redirect', 'votretheme_redirect_cpt_archive' );
 ?>

Alternatively, if you do not want to create a template page and a redirect, simply create an archvie page named with your Post Type such as Team which would translate to archive-equipe.php.

In this case, a redirect is not necessary as WordPress will automatically use the archive-equipe.php template.

You can use the same condition for other customed archives. If you do not use a Post Type template, WordPress will use the archive.php and its loop.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>