• Resolved jnz31

    (@jnz31)


    hi

    thanks for this awesome tool, helps a lot, while developing. i have a very small issue, no big deal, but i thought i’d report it.
    if i switch the template via functions.php, the “template name” field is empty. i tried to change the add_action prority, but that did not help. absolutly no big deal, the list works just fine.
    thank you for this tool

    here is the code, i use in functions.php to change the template, based on the parent page ID:

    function is_child( $pid ) {
    	global $post;
    	$ancestors = get_post_ancestors( $post->$pid );
    	$root = count( $ancestors ) - 1;
    	$parent = $ancestors[$root];
    	if( is_page() && ( is_page( $pid ) || $post->post_parent === $pid || in_array( $pid, $ancestors ) ) ) {
    		return true;
    	} else {
    		return false;
    	}
    };
    
    function switch_template( $tpl ) {
    	$template = get_stylesheet_directory() . "/" . $tpl;
    	if ( file_exists( $template ) ) {
    		load_template( $template );
    		exit;
    	}
    };
    
    function wpse140605_template_redirect() {
    	if ( is_child( 20 ) || is_child( 22 ) ) { // lookbook
    		switch_template( 'single-lookbook.php' );
    	}
    };
    add_action( 'template_redirect', 'wpse140605_template_redirect', 10, 0 );

    https://www.ads-software.com/plugins/show-current-template/

Viewing 1 replies (of 1 total)
  • Thread Starter jnz31

    (@jnz31)

    ok. i just learned, that template_redirect is the wrong approach..

    here is the correct way to solve this.

Viewing 1 replies (of 1 total)
  • The topic ‘template info is empty if load_template()’ is closed to new replies.