- shares
- Facebook Messenger
- Gmail
- Viber
- Skype
Định hướng sử dụng template ngoài các template mặc định bao gồm: index.php, single.php, sidebar.php, ..
Thay thế template mặc định bạn sử dụng filter “template_include”. Bằng cách này có thể custom default template. Copy đoạn code dưới đây vào functions.php
add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {
if ( is_page( 'portfolio' ) ) {
$new_template = locate_template( array( 'portfolio-page-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
Ví dụ khác:
add_filter( 'template_include', 'wpse_100854_switch_template' );
function wpse_100854_switch_template( $template )
{
if ( empty ( $_GET['template'] )
return $false;
// do not allow to break out of the directory
$new = ltrim( $_GET['template'], '.' );
$path = get_template_directory() . "/templates/$new.php";
if ( file_exists( $path ) )
return $path;
return $template;
}
Xem cách chuyển hướng URL sử dụng template_redirect.
Tác giả: hoangweb.com
Để nhận được bài viết mới vui lòng đăng ký kênh kiến thức WordPress từ A-Z ở Form bên dưới. Bạn cũng có thể nhận được sự trợ giúp trên Twitter và Facebook
- shares
- Facebook Messenger
- Gmail
- Viber
- Skype