WordPress-将网址重定向到单个模板-{post_type}-{other} .php?

时间:2019-03-01 06:18:19

标签: php wordpress

有一个custom_post_type = "post_type"有一个帖子输出template = "single-post_type.php"

有必要在单击链接 mywebsite.com/post_type/post_name/second 时,不会加载single-post_type.php,例如single-post_type-second.php

在这种情况下,您必须保存single-post_type.php

2 个答案:

答案 0 :(得分:0)

决定-template_include

add_filter( 'template_include', 'include_post_type_second' );
function include_post_type_second( $template ) {
    if ( (in_array('second', explode('/', $_SERVER['REQUEST_URI']))) ) {
        if ( $new_template = locate_template( array( 'single-post_type-second.php' ) ) )
            $template = $new_template ;
    }
    return $template;
}

答案 1 :(得分:0)

您可以使用此代码制作其他自定义页面类型:

add_filter( 'template_include', 'another_post_type' );
function another_post_type( $template ) {
    if ( (in_array('second', explode('/', $_SERVER['REQUEST_URI']))) ) {
        if ( $template2 = locate_template( array( 'single-post_type-second.php' ) ) )
            $template1 = $template2 ;
    }
    return $template1;
}