• Resolved wpfan1000

    (@wpfan1000)


    Hi, I realize this may sound like a trivial thing to do, but in some themes one needs to copy and rename 2 standard php files, and edit code in one of them. It is actually quite involved.

    Could you please provide steps to create a template for a CPT eg with slug “mycpt”?

    In my case I need to add code to a template to display custom fields of a CPT.

    Thanks ahead of time.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Hi,
    Thanks for posting.

    You have some options and overriding or creating templates is usually not what I suggest. It means you become responsible to update/manage those templates.

    Instead, I would suggest hooking in what you want through hooks in the theme.

    For example, if you wanted to hook in a ACF custom field at the top of your custom post content you can use a hook like this:

    add_action( 'kadence_single_before_entry_content', 'custom_post_type_meta_add' );
    function custom_post_type_meta_add() {
    global $post;
    if ( is_singular( 'mycpt' ) ) {
    echo get_post_meta( $post->ID, 'key', true );
    }
    }

    You can add things like this using a codesnippets plugin: https://kadence-theme.com/knowledge-base/advanced/how-to-add-a-custom-filter-or-function-with-code-snippets/

    There are lots of hooks throughout the theme that you can use so that is my suggested route.

    If you want to use templates you certainly can. The theme doesn’t change how single post templates work so you can adding single-mycpt.php and place anything you want in there and build out the page from the ground up.

    I hope that helps!

    Ben

    Thread Starter wpfan1000

    (@wpfan1000)

    Hi Ben,

    That is VERY helpful! Thanks ??

    That is the first time I have come across the idea of using a hook to display custom fields for a particular CPT.

    I think that is a very nice solution. Simpler than modifying a template.

    Some other themes require two templates to be created, because their single.php calls a secondary template. And the code in the calling template needs to be changed. In this case, using a hook is particularly attractive.

    Thread Starter wpfan1000

    (@wpfan1000)

    Hi Ben,

    I am very interested in using Kadence because of your suggestion above (of using a hook vs creating templates) to display custom fields, etc.

    I wanted to learn more about Kadence hooks and so I searched your KB and only found:

    One of the features that Kadence Theme Pro adds to the Kadence theme is called Hooked Elements (or Element Hooks)

    Please let me know:

    1)
    Can your suggestion above of using

    add_action( 'kadence_single_before_entry_content', 'custom_post_type_meta_add' );
    function custom_post_type_meta_add() {
    global $post;
    if ( is_singular( 'mycpt' ) ) {
    echo get_post_meta( $post->ID, 'key', true );
    }
    }

    be used in the free version?

    2)
    Is there a list and description of hooks in the docs?

    Thanks again ahead of time…..

    hannah

    (@hannahritner)

    Hi @wpfan1000,
    Apologies for the delay!
    1. Yes, you can use this in the free version.

    2. Kadence Hooks are only in the Pro Add-On. You can learn more here: https://kadence-theme.com/knowledge-base/pro-addon/how-to-use-element-hooks/

    Hope that’s helpful!

    Best,
    Hannah

    Thread Starter wpfan1000

    (@wpfan1000)

    Hi Hannah,

    1. Thanks for confirming that.

    2. I think we currently have different definitions of what a hook is. Ben says above:

    For example, if you wanted to hook in a ACF custom field at the top of your custom post content you can use a hook like this:

    add_action( ‘kadence_single_before_entry_content’, ‘custom_post_type_meta_add’ );
    function custom_post_type_meta_add() {

    So he is referring to:

    add_action( ‘kadence_single_before_entry_content’, ‘custom_post_type_meta_add’ );

    as a hook.

    And this is the way I mean it as well.

    So to say in 1) Yes you can use this (a hook) in the free version and in 2) say hooks are only in the Pro addon does not make sense to me.

    Furthermore when I mean a list of hooks in the documentation, I mean eg the hook:

    add_action( ‘kadence_single_before_entry_content’, ‘custom_post_type_meta_add’ );

    listed and described with the other hooks available.

    As far as I can tell:

    Kadence Hooks in the Pro Add-On. You can learn more here: https://kadence-theme.com/knowledge-base/pro-addon/how-to-use-element-hooks/

    is a GUI for using hooks. It is not the hooks themselves.

    Thread Starter wpfan1000

    (@wpfan1000)

    Hi Hannah,

    As an example of what I am looking for in terms of a list of hooks in the documentation:

    https://docs.generatepress.com/collection/hooks/

    Thanks

    Here is a list I just added for you: https://kadence-theme.com/knowledge-base/advanced/theme-hooks/

    Ben

    Thread Starter wpfan1000

    (@wpfan1000)

    Wow Ben, thanks very much for doing that.

    I did not mean to imply you should make that list for me. I was more wondering if it was available already.

    But I appreciate you having done it and it gives me a better idea of the capabilities of the theme.

    That’s great. Aldo someone did a nice plugin that show hooks in place.

    Thread Starter wpfan1000

    (@wpfan1000)

    Thanks @fmarconi for pointing that out.
    That’s very helpful.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to create a template for a CPT’ is closed to new replies.