最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Hook to override title, image and content

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked Apr 12, 2020 at 17:36 mrKC.988mrKC.988 1072 silver badges14 bronze badges 2
  • 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 Commented Apr 13, 2020 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 Commented Apr 13, 2020 at 9:48
Add a comment  | 

1 Answer 1

Reset to default 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;
}
发布评论

评论列表(0)

  1. 暂无评论