2

I can control the custom template to be used on custom post type pages but the template file would still need to have the markup of the theme. I was wondering if I could make it theme independent?

Example: I have a plugin that registers my custom post type. I have the post meta info that I want to show in a custom template. I have that template working on my custom post type pages but the max I could do is to use get_header() & get_footer() calls but I can't deal out the need of having the theme markup between header & footer.

Did that make any sense? Any ideas?

3

I have got it working by the following code:

add_action( 'template_redirect', 'ft_job_cpt_template' );

function ft_job_cpt_template() {
    global $wp, $wp_query;

    if ( isset( $wp->query_vars['post_type'] ) && $wp->query_vars['post_type'] == 'job' ) {
        if ( have_posts() ) {
            add_filter( 'the_content', 'ft_job_cpt_template_filter' );
        }
        else {
            $wp_query->is_404 = true;
        }
    }
}

function ft_job_cpt_template_filter( $content ) {

    global $wp_query;
    $jobID = $wp_query->post->ID;

    $output = ''; // Build markup fetching info from postmeta

    return $output;
}

Feel free to comment on any suggestions.

  • 1
    Why not just hook onto the_content and check the post type there? - just a bit cleaner :) – Stephen Harris Sep 27 '12 at 9:33
  • @StephenHarris Kicking in the filter only when needed makes more sense to me here, and I find this a lot cleaner. Consider if you have lot of stuff going on, it would be better to just have a function which kicks in or kicks out certain things all from one place. And this also makes sure if a certain content is processed a large no of times, it doesn't go under the filter call just to check if it should do anything or not. A bit of performance gain too. And you can reuse filters in a better way this way, because filter just filters, and not judge conditions :) – Ashfame Oct 24 '12 at 8:49
  • Pretty cool so where is the template actually exist? Or is it all to be built in $output = ''; – Bryan Willis Aug 16 '14 at 3:34
  • @willow Use locate_template() and load_template(), Here is a code sample on how you can do that - codex.wordpress.org/Function_Reference/load_template – Ashfame Aug 17 '14 at 8:15

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.