WordPress 插件开发 增加自定义页面类型及前端显示页面 Add Custom page template with WordPress plugin

前篇我们讲到WordPress 插件开发 增加自定义页面类型及前端显示页面,下面我们记录一下简单的方法来达到同样的效果:

add_filter( 'page_template', 'jpage_template' );
function jpage_template( $page_template ){

    if ( get_page_template_slug() == 'template-configurator.php' ) {
        $page_template = dirname( __FILE__ ) . '/template-configurator.php';
    }
    return $page_template;
}

/**
 * Add "Custom" template to page attirbute template section.
 */
add_filter( 'theme_page_templates', 'jadd_template_to_select', 10, 4 );
function jadd_template_to_select( $post_templates, $wp_theme, $post, $post_type ) {

    // Add custom template named template-custom.php to select dropdown 
    $post_templates['template-configurator.php'] = __('Configurator');

    return $post_templates;
}

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注