1

I want to add a custom template for a custom post ('post_type' => 'matches').

I am writing a plugin.I keep my template in the templates/fp-fixture-template.php file.

Here is the function:

function fixture_template( $template_path ) { 
    if ( get_post_type() == 'matches' ) {
        if ( is_single() ) { 
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin 
            if ( $theme_file = locate_template( array ( 'fp-fixture-template.php' ) ) ) { 
                $template_path = $theme_file; 
            } else { 
                $template_path = plugin_dir_path( FILE ) 
                    . 'templates/fp-fixture-template.php'; 
            } 
        } 
    } 
    return $template_path; 
} 

Here is the filter:

add_filter( 'template_include', 'fixture_template');

Code in the fp-fixture-template.php file:

<?php
 /*Template Name: Matches Template
 */
 if ( ! defined( 'ABSPATH' ) ) exit; // Don't allow direct access
?>
<div id="primary">
    <div id="content" role="main">
    <?php
    $mypost = array( 'post_type' => 'matches', );
    $loop = new WP_Query( $mypost );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
        <?php endwhile; ?>
    </div>      
</div>
4

This plugin looks for page_contact.php from active theme's folder and uses plugin's templates/page_contact.php as a fallback.

<?php
/**
 * Plugin Name: Test Plugin
 */

add_filter( 'template_include', 'contact_page_template', 99 );

function contact_page_template( $template ) {
    $file_name = 'page_contact.php';

    if ( is_page( 'contact' ) ) {
        if ( locate_template( $file_name ) ) {
            $template = locate_template( $file_name );
        } else {
            // Template not found in theme's folder, use plugin's template as a fallback
            $template = dirname( __FILE__ ) . '/templates/' . $file_name;
        }
    }

    return $template;
}

Create page "Contact" and then templates/page_contact.php in your plugins folder. Now you should see your template content instead of normal template.

  • Could you please tell me how to do that?Or Could you please share your code? – bdtheme Nov 10 '15 at 17:27
  • okay I added this code in my template file but shows no page found??<pre> <?php /*Template Name: Matches Template */ ?> <div id="primary"> <div id="content" role="main"> <?php $mypost = array( 'post_type' => 'matches', ); $loop = new WP_Query( $mypost ); while ( $loop->have_posts() ) : $loop->the_post(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <?php endwhile; ?> </div> </div></pre> – bdtheme Nov 12 '15 at 2:03
  • is_page or is_post? – bdtheme Nov 12 '15 at 2:18
  • I would suggest getting a firm understanding of how filters work in general. A filter is a modification to an existing set of responses from within the wordpress core's platform. You would put this piece of code into your functions.php located in your theme folder and it should work appropriately. – Ryan Dennler Nov 6 '17 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.