Javascript,Nodejs,MongodDB,WordPress,CSS,PHP

LightBlog

Saturday, December 8, 2018

How to override template in wordpress

sometimes our design is different to other page then we create a template
and select template from inside page where we want to show but sometimes we don't want to select template from page just override default template when created page.
This filter hook is executed immediately before WordPress includes the predetermined template file. This can be used to override WordPress's default template behavior.

syntax:
add_filters( 'template_include', string $template )

template_include: hooks name
$template: (string) The path of the template to include.
99: is priority

where to write this code ?

inside functions.php of your active theme or write inside plugin

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

function portfolio_page_template( $template ) {

if ( is_page( 'portfolio' ) ) {  // page slug
$new_template = locate_template( array( 'portfolio-page-template.php' ) ); // page name
if ( !empty( $new_template ) ) {
return $new_template;
}
}

return $template;

}

No comments:

Post a Comment