I have created a wordpress plugin where i create many things including a custom post type, I have created my own template for the post type too which all works great.
The only problem is that the template is being applied to every post on the site even though i am specifying the post type I have supplied the code below where i apply the template to the post type, hope someone can help me out.
<?php
/**
* @package WhitepaperPlugin
*/
namespace Inc\Base;
use Inc\Base\BaseController;
class LoadArchives extends BaseController{
public function register(){
add_action( 'archive_template', array($this,'sponsors_archive_template') );
add_action( 'archive_template', array($this,'downloads_archive_template') );
add_action( 'single_template', array($this,'download_custom_template') );
add_action( 'page_template', array($this,'wpa3396_page_template') );
$RegistrationFormId = esc_attr( get_option( 'ik_form_id' ) );
}
public function sponsors_archive_template( $archive_template ) {
$archive_template = $this->plugin_path . 'page-templates/taxonomy-sponsors.php';
return $archive_template;
}
function wpa3396_page_template( $page_template ) {
if ( is_page( 'sponsors' ) ) {
$page_template = $this->plugin_path . 'page-templates/template-sponsors.php';
}
return $page_template;
}
public function downloads_archive_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'downloads' ) ) {
$archive_template = $this->plugin_path . 'page-templates/archive-downloads.php';
}
return $archive_template;
}
public function download_custom_template( $single ) {
global $post;
if ($post->post_type == "downloads" && $template !== locate_template(array( $this->plugin_path . 'page-templates/single-downloads.php' ))){
return $this->plugin_path . 'page-templates/single-downloads.php';
}
return $template;
}
}