1

I'm creating a custom post type with a plugin, and I want to show my cpt title/thumbnail/content... etc using hooks (assuming it's better than loading a custom template).

But themes are showing title/thumbnail/content with their single.php template...... So is there a hook that I can use to override all that, or do I need to use 2 hooks to remove title/image, and a 3rd hook to override the content with my cpt content?

I also plan to add custom meta fields in my cpt.

How would you do this?

  • Why are you assuming this? assuming it's better than loading a custom template I use custom templates all the time and have not had a problem. It's how WordPress is designed. – rudtek Apr 13 at 3:46
  • its for a plugin, not a theme... my template will have problems with some of the themes and I want to avoid that – mrKC.988 Apr 13 at 9:48
1

If you created your cpt correctly and just want it do be displayed like any other post, there is nothing else you need to do. WordPress will use the single.php from the theme and display the single versions of your post type as a normal post.

If you want to add additional meta, you could do this with a function and a filter. for instance to add your meta after content you this:

function rt_before_after($content) {
    if(is_singular('your_post_type')) {
        $beforecontent = '',  //what you want before ie meta
        $aftercontent = '', //what you want after  ie custom meta
        $fullcontent = $beforecontent . $content . $aftercontent;
        return $fullcontent;

    } else {
        return $content;
    }
}
add_filter('the_content', 'rt_before_after');

If you are trying to customize the display of your CPT it's going to be a bit more difficult.

Every theme is going to use their own hooks... if they use them at all. You're playing a guessing game as to what they may or may not be doing for each theme. Seems easier to make sure they they use your data with a template. Here's what I use: (you'd need to alter the code to fit your CPT name and template location)

function rt_load_custom_template( $template ) {

if ( is_singular( 'your_post_type' ) ) {
    $template = plugins_url( 'templates/your_post_type.php', __FILE__ );
}

add_filter( 'single_template', 'rt_load_custom_template', 50, 1 );
    return $template;
}
| improve this answer | |

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.