0

I am working on a plugin. In plugin, i create the files that move into theme folder after plugin activate. The files move in active theme successfully.

I use template_include to include my files from theme. I create a page template file for listing page 'my_items.php' and archive for page 'archive-my_items.php' and for tags page 'taxonomy-my_tags.php' and for single 'single-my_items.php'. All files working perfectly except 'single-my_itmes.php'. When i click on my items page, the listing page open, when i click on tags, the tag file open but when i click on view more from listing page its redirect listing page again, the link on address bar is single page link but it access the listing page.

Here is my code:

add_filter('template_include', 'foo_template_chooser');
function foo_template_chooser($template){
    global $wp;
    $plugindir = dirname(__FILE__);

    if( $wp->query_vars["post_type"] == 'my_items' ){
        if(file_exists(TEMPLATEPATH . '/my_items/archive-my_items.php')) {
            return locate_template('my_items/archive-my_items.php');
        } else {
            return $plugindir . '/template/my_items.php';
        }

        if(file_exists(TEMPLATEPATH . '/my_items/my_items.php')) {
            return locate_template('my_items/my_items.php');
        } else {
            return $plugindir . '/template/my_items.php';
        }
    }

    if( $wp->query_vars["post_type"] == 'my_items' && is_single() ){
        if(file_exists(TEMPLATEPATH . '/my_items/single-my_items.php')) {
            return locate_template('my_items/single-my_items.php');
        } else {
            return $plugindir . '/template/single-my_items.php';
        }
    }

    if ( is_tax('my_tags') ) {
        if(file_exists(TEMPLATEPATH . '/my_items/taxonomy-my_tags.php')) {
            return locate_template('my_items/taxonomy-my_tags.php');
        } else {
            return $plugindir . '/template/taxonomy-my_tags.php';
        }
    }

    return $template;   
}

When i Comment my this code:

/*if( $wp->query_vars["post_type"] == 'my_items' ){
        if(file_exists(TEMPLATEPATH . '/my_items/archive-my_items.php')) {
            return locate_template('my_items/archive-my_items.php');
        } else {
            return $plugindir . '/template/my_items.php';
        }

        if(file_exists(TEMPLATEPATH . '/my_items/my_items.php')) {
            return locate_template('my_items/my_items.php');
        } else {
            return $plugindir . '/template/my_items.php';
        }
    }*/

Then the single page work but listing page shows WordPress default layout. So what can i do please guide me.

0

Ok i got answer by my own

I just put the single file condition above the file template

Here is my code:

add_filter('template_include', 'foo_template_chooser');
function foo_template_chooser($template){
    global $wp;
    $plugindir = dirname(__FILE__);

    if( $wp->query_vars["post_type"] == 'my_items' && is_single() ){
        if(file_exists(TEMPLATEPATH . '/my_items/single-my_items.php')) {
            return locate_template('my_items/single-my_items.php');
        } else {
            return $plugindir . '/template/single-my_items.php';
        }
    }

    if( $wp->query_vars["post_type"] == 'my_items' ){
        if(file_exists(TEMPLATEPATH . '/my_items/archive-my_items.php')) {
            return locate_template('my_items/archive-my_items.php');
        } else {
            return $plugindir . '/template/my_items.php';
        }

        if(file_exists(TEMPLATEPATH . '/my_items/my_items.php')) {
            return locate_template('my_items/my_items.php');
        } else {
            return $plugindir . '/template/my_items.php';
        }
    }

    if ( is_tax('my_tags') ) {
        if(file_exists(TEMPLATEPATH . '/my_items/taxonomy-my_tags.php')) {
            return locate_template('my_items/taxonomy-my_tags.php');
        } else {
            return $plugindir . '/template/taxonomy-my_tags.php';
        }
    }

    return $template;   
}

And its working perfectly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.